phppkg/phpgit

PHP 8.1+ 的 Git 封装库

v2.0.4 2022-11-05 11:50 UTC

README

GitHub tag (latest SemVer) Php Version Unit Tests Deploy Pages

中文说明

PhpGit - 💪 PHP 编写的 Git 封装和功能扩展库。

该项目是从 https://github.com/kzykhys/PHPGit 分支出来的

特性

  • 快速运行 git 命令,例如:clone,add,commit,merge
  • Git 仓库信息检索:status branch remote
  • 通过 git log 生成变更日志

安装

要求

  • PHP 8.1+
  • Git

方法 1:直接 composer require

composer require phppkg/phpgit

方法 2:更新 composer.json

更新 composer.json 并运行 composer update

{
    "require": {
        "phppkg/phpgit": "dev-master"
    }
}

基本用法

<?php

require __DIR__ . '/vendor/autoload.php';

$git = PhpGit\Git::new();
$git->clone('https://github.com/phppkg/PhpGit.git', '/path/to/repo');
$git->setRepository('/path/to/repo');
$git->remote->add('production', 'git://example.com/your/repo.git');

$git->add('README.md');
$git->commit('Adds README.md');

$git->checkout('release');
$git->merge('master');

$git->push();
$git->push('production', 'release');

$git->tag->create('v1.0.1', 'release');

foreach ($git->tree('release') as $object) {
    if ($object['type'] == 'blob') {
        echo $git->show($object['file']);
    }
}

Git 信息

$repo = PhpGit\Repo::new('/path/to/repo');

$remotes = $repo->getRemotes();

$url  = $repo->getRemoteUrl('origin');
$info = $repo->getRemoteInfo('origin');

var_dump($info);

输出

object(PhpGit\Info\RemoteInfo)#35 (8) {
  ["type"]=> string(4) "http"
  ["name"]=> string(6) "origin"
  ["url"]=> string(34) "https://github.com/phppkg/phpgit.git"
  ["scheme"]=> string(5) "https"
  ["host"]=> string(10) "github.com"
  ["path"]=> string(11) "phppkg/phpgit"
  ["group"]=> string(4) "ulue"
  ["repo"]=> string(6) "phpgit"
}

变更日志

提供快速生成格式化变更日志的功能。

格式化器

  • SimpleFormatter
  • MarkdownFormatter
  • GithubReleaseFormatter

过滤器

  • KeywordFilter
  • KeywordsFilter
  • MsgLenFilter
  • WordsLenFilter

示例

use Toolkit\Cli\Color;
use PhpGit\Changelog\Formatter\GithubReleaseFormatter;
use PhpGit\Changelog\Formatter\SimpleFormatter;
use PhpGit\Changelog\GitChangeLog;

// this is built in log format.
// you can custom format string, but must be set an log parser by $gcl->setLineParser(new YourLineParser);
$logFormat = GitChangeLog::LOG_FMT_HSC;

$oldVersion = 'v0.2.1';
$newVersion = 'HEAD';

// get output by git log cmd:
//  `git log v0.2.1...HEAD --reverse --pretty=format:"%H | %s | %cn" --no-merges`
$c = PhpGit\Git::new()->newCmd('log');
$c->add("$oldVersion...$newVersion");
$c->add('--reverse');
$c->addf('--pretty=format:"%s"', $logFormat);

// get repo url
$info = PhpGit\Repo::new()->getRemoteInfo('origin');
$repoUrl = $info->getHttpUrl();
Color::info('repo URL: ' . $repoUrl);

// create object by output.
$gcl = GitChangeLog::new($c->getOutput());
$gcl->setLogFormat($logFormat);
$gcl->setRepoUrl($repoUrl);

// you can set output style. default is markdown.
// can also, you can custom your item formatter
$gcl->setItemFormatter(new GithubReleaseFormatter());
//$gcl->setItemFormatter(new SimpleFormatter());

Color::info('parse logs and generate changelog');

// parse and generate.
$str = $gcl->generate();

echo $str;

运行示例 php bin/chlog.php 将看到

> git log v0.2.1...HEAD --reverse --pretty=format:"%H | %s | %cn" --no-merges
> git remote -v
[INFO] repo URL:https://github.com/phppkg/phpgit
[INFO] parse logs and generate changelog

### Fixed

 - fix get latest tag error on windows https://github.com/phppkg/phpgit/commit/b9892b0ec363e405fcb76b08ea971fb651b4d2dc

### Update

 - up: rename package org to phppkg https://github.com/phppkg/phpgit/commit/990e55c6beddf654819c323c2a18d329074399f9
 - update some info https://github.com/phppkg/phpgit/commit/"1110de8b5ef0406c837bcd65f607b6f9483c9154

### Feature

 - feat: add util for quick generate change log https://github.com/phppkg/phpgit/commit/50962c12d3f16cdbbd8c1f21bc17ff920842365e

API

Git 命令

  • git add
    • $git->add(string|array|\Traversable $file, array $options = [])
  • git archive
    • $git->archive(string $file, string $tree = null, string|array|\Traversable $path = null, array $options = [])
  • git branch
    • $git->branch(array $options = [])
    • $git->branch->create(string $branch, string $startPoint = null, array $options = [])
    • $git->branch->move(string $branch, string $newBranch, array $options = [])
    • $git->branch->delete(string $branch, array $options = [])
  • git cat-file
    • $git->cat->blob(string $object)
    • $git->cat->type(string $object)
    • $git->cat->size(string $object)
  • git checkout
    • $git->checkout(string $branch, array $options = [])
    • $git->checkout->create(string $branch, string $startPoint = null, array $options = [])
    • $git->checkout->orphan(string $branch, string $startPoint = null, array $options = [])
  • git clone
    • $git->clone(string $repository, string $path = null, array $options = [])
  • git commit
    • $git->commit(string $message, array $options = [])
  • git config
    • $git->config(array $options = [])
    • $git->config->set(string $name, string $value, array $options = [])
    • $git->config->add(string $name, string $value, array $options = [])
  • git describe
    • $git->describe(string $committish = null, array $options = [])
    • $git->describe->tags(string $committish = null, array $options = [])
  • git fetch
    • $git->fetch(string $repository, string $refspec = null, array $options = [])
    • $git->fetch->all(array $options = [])
  • git init
    • $git->init(string $path, array $options = [])
  • git log
    • $git->log(string $revRange = '', string $path = null, array $options = [])
  • git merge
    • $git->merge(string|array|\Traversable $commit, string $message = null, array $options = [])
    • $git->merge->abort()
  • git mv
    • $git->mv(string|array|\Iterator $source, string $destination, array $options = [])
  • git pull
    • $git->pull(string $repository = null, string $refspec = null, array $options = [])
  • git push
    • $git->push(string $repository = null, string $refspec = null, array $options = [])
  • git rebase
    • $git->rebase(string $upstream = null, string $branch = null, array $options = [])
    • $git->rebase->continues()
    • $git->rebase->abort()
    • $git->rebase->skip()
  • git remote
    • $git->remote()
    • $git->remote->add(string $name, string $url, array $options = [])
    • $git->remote->rename(string $name, string $newName)
    • $git->remote->rm(string $name)
    • $git->remote->show(string $name)
    • $git->remote->prune(string $name = null)
    • $git->remote->head(string $name, string $branch = null)
    • $git->remote->head->设置(字符串 $name, 字符串 $branch)
    • $git->remote->head->删除(字符串 $name)
    • $git->remote->head->remote(字符串 $name)
    • $git->remote->branches(字符串 $name, 数组 $branches)
    • $git->remote->branches->设置(字符串 $name, 数组 $branches)
    • $git->remote->branches->添加(字符串 $name, 数组 $branches)
    • $git->remote->url(字符串 $name, 字符串 $newUrl, 字符串 $oldUrl = null, 数组 $options = [])
    • $git->remote->url->设置(字符串 $name, 字符串 $newUrl, 字符串 $oldUrl = null, 数组 $options = [])
    • $git->remote->url->添加(字符串 $name, 字符串 $newUrl, 数组 $options = [])
    • $git->remote->url->删除(字符串 $name, 字符串 $url, 数组 $options = [])
  • git 重置
    • $git->reset(字符串|数组|\Traversable $paths, 字符串 $commit = null)
    • $git->reset->软重置(字符串 $commit = null)
    • $git->reset->混合重置(字符串 $commit = null)
    • $git->reset->硬重置(字符串 $commit = null)
    • $git->reset->合并重置(字符串 $commit = null)
    • $git->reset->保持重置(字符串 $commit = null)
    • $git->reset->模式重置(字符串 $mode, 字符串 $commit = null)
  • git 删除
    • $git->rm(字符串|数组|\Traversable $file, 数组 $options = [])
    • $git->rm->cached(字符串|数组|\Traversable $file, 数组 $options = [])
  • git 短日志
    • $git->shortlog(字符串|数组|\Traversable $commits = HEAD)
    • $git->shortlog->summary(字符串 $commits = HEAD)
  • git 查看
    • $git->show(字符串 $object, 数组 $options = [])
  • git 保存工作现场
    • $git->stash()
    • $git->stash->save(字符串 $message = null, 数组 $options = [])
    • $git->stash->lists(数组 $options = [])
    • $git->stash->show(字符串 $stash = null)
    • $git->stash->drop(字符串 $stash = null)
    • $git->stash->pop(字符串 $stash = null, 数组 $options = [])
    • $git->stash->apply(string $stash = null, array $options = [])
    • $git->stash->branch(string $name, string $stash = null)
    • $git->stash->clear()
    • $git->stash->create()
  • git status
    • $git->status(array $options = [])
  • git tag
    • $git->tag()
    • $git->tag->create(string $tag, string $commit = null, array $options = [])
    • $git->tag->delete(string|array|\Traversable $tag)
    • $git->tag->verify(string|array|\Traversable $tag)
  • git ls-tree
    • $git->tree(string $branch = master, string $path = '')

git add

$git->add(string|array|\Traversable $file, array $options = [])

将文件内容添加到索引

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->add('file.txt');
$git->add('file.txt', ['force' => false, 'ignore-errors' => false);
选项
  • force (boolean) 允许添加其他情况下被忽略的文件
  • ignore-errors (boolean) 不要中止操作

git archive

$git->archive(string $file, string $tree = null, string|array|\Traversable $path = null, array $options = [])

从命名的树创建一个存档

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->archive('repo.zip', 'master', null, ['format' => 'zip']);
选项
  • format (boolean) 结果存档的格式:tar 或 zip
  • prefix (boolean) 在存档中的每个文件名前添加前缀/

git branch

$git->branch(array $options = [])

返回远程跟踪分支和本地分支的数组

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$branches = $git->branch();
输出示例
[
    'master' => ['current' => true, 'name' => 'master', 'hash' => 'bf231bb', 'title' => 'Initial Commit'],
    'origin/master' => ['current' => false, 'name' => 'origin/master', 'alias' => 'remotes/origin/master']
]
选项
  • all (boolean) 列出远程跟踪分支和本地分支
  • remotes (boolean) 列出远程跟踪分支

$git->branch->create(string $branch, string $startPoint = null, array $options = [])

创建一个名为 $branch 的新分支头,它指向当前 HEAD,如果提供了 $startPoint,则指向 $startPoint

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->branch->create('bugfix');              // from current HEAD
$git->branch->create('patch-1', 'a092bf7s'); // from commit
$git->branch->create('1.0.x-fix', 'v1.0.2'); // from tag
选项
  • force (boolean) 如果 $branch 已经存在,将 $branch 重置为 $startPoint

$git->branch->move(string $branch, string $newBranch, array $options = [])

移动/重命名分支及其相应的reflog

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->branch->move('bugfix', '2.0');
选项
  • force (boolean) 即使新分支名已存在,也移动/重命名分支

$git->branch->delete(string $branch, array $options = [])

删除分支

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->branch->delete('2.0');

该分支必须完全合并到其上游分支中,或者如果没有设置 --track 或 --set-upstream,则合并到 HEAD 中。

选项
  • force (boolean) 不考虑其合并状态,删除分支

git cat-file

$git->cat->blob(string $object)

返回 blob 对象的内容

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$contents = $git->cat->blob('e69de29bb2d1d6434b8b29ae775ad8');

$git->cat->type(string $object)

返回由 $object 标识的对象类型

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$type = $git->cat->type('e69de29bb2d1d6434b8b29ae775ad8');

$git->cat->size(string $object)

返回由 $object 标识的对象大小

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$type = $git->cat->size('e69de29bb2d1d6434b8b29ae775ad8');

git checkout

$git->checkout(string $branch, array $options = [])

通过更新索引、工作树和 HEAD 来切换分支,以反映指定的分支或提交

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->checkout('develop');
选项
  • force (boolean) 即使索引或工作树与 HEAD 不同,也继续进行
  • merge (boolean) 合并本地修改

$git->checkout->create(string $branch, string $startPoint = null, array $options = [])

创建新分支并检出

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->checkout->create('patch-1');
$git->checkout->create('patch-2', 'develop');
选项
  • force (boolean) 即使索引或工作树与 HEAD 不同,也继续进行

$git->checkout->orphan(string $branch, string $startPoint = null, array $options = [])

创建一个名为 <new_branch> 的新孤儿分支,从 <start_point> 开始,并切换到该分支

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->checkout->orphan('gh-pages');
选项
  • force (boolean) 即使索引或工作树与 HEAD 不同,也继续进行

git clone

$git->clone(string $repository, string $path = null, array $options = [])

将仓库克隆到新的目录

$git = new PhpGit\Git();
$git->clone('https://github.com/kzykhys/PhpGit.git', '/path/to/repo');
选项
  • shared (boolean) 默认没有自己的对象
  • bare (boolean) 创建一个裸 Git 仓库

git commit

$git->commit(string $message, array $options = [])

将更改记录到仓库

$git = new PhpGit\Git();
$git->clone('https://github.com/kzykhys/PhpGit.git', '/path/to/repo');
$git->setRepository('/path/to/repo');
$git->add('README.md');
$git->commit('Fixes README.md');
选项
  • all (boolean) 状态已修改和已删除的文件
  • reuse-message (string) 使用现有提交对象,并在创建提交时重用日志消息和作者信息(包括时间戳)
  • squash (string) 为 rebase --autosquash 构建提交消息
  • author (string) 覆盖提交作者
  • date (string) 覆盖提交中使用的作者日期
  • cleanup (string) 可以是 verbatim、whitespace、strip 和 default 之一
  • amend (boolean) 用于修改当前分支的尖端

git config

$git->config(array $options = [])

返回在配置文件中设置的 所有变量

选项
  • global (boolean) 读取或写入当前用户的配置选项
  • system (boolean) 读取或写入当前机器上所有用户的配置选项

$git->config->set(string $name, string $value, array $options = [])

设置选项

选项
  • global (boolean) 读取或写入当前用户的配置选项
  • system (boolean) 读取或写入当前机器上所有用户的配置选项

$git->config->add(string $name, string $value, array $options = [])

在不更改任何现有值的情况下向选项添加新行

选项
  • global (boolean) 读取或写入当前用户的配置选项
  • system (boolean) 读取或写入当前机器上所有用户的配置选项

git describe

$git->describe(string $committish = null, array $options = [])

返回从提交可到达的最新的标签

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->tag->create('v1.0.0');
$git->commit('Fixes #14');
echo $git->describe('HEAD', ['tags' => true]);
输出示例
v1.0.0-1-g7049efc
选项
  • all (boolean) 启用匹配任何已知的分支、远程跟踪分支或轻量级标签
  • tags (boolean) 启用匹配轻量级(非注释)标签
  • always (boolean) 显示唯一的缩写提交对象作为后备

$git->describe->tags(string $committish = null, array $options = [])

等价于 $git->describe($committish, ['tags' => true]);

git fetch

$git->fetch(string $repository, string $refspec = null, array $options = [])

从一个或多个其他仓库获取命名头或标签,以及完成它们所必需的对象

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'git://your/repo.git');
$git->fetch('origin');
选项
  • append (boolean) 将获取的引用的名称和对象名称追加到 .git/FETCH_HEAD 的现有内容中
  • keep (boolean) 保持下载的包
  • prune (boolean) 在获取后,删除任何远程跟踪分支,这些分支在远程不再存在

$git->fetch->all(array $options = [])

获取所有远程仓库

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'git://your/repo.git');
$git->remote->add('release', 'git://your/another_repo.git');
$git->fetch->all();
选项
  • append (boolean) 将获取的引用的名称和对象名称追加到 .git/FETCH_HEAD 的现有内容中
  • keep (boolean) 保持下载的包
  • prune (boolean) 在获取后,删除任何远程跟踪分支,这些分支在远程不再存在

git init

$git->init(string $path, array $options = [])

创建一个空的 git 仓库或重新初始化现有的一个

$git = new PhpGit\Git();
$git->init('/path/to/repo1');
$git->init('/path/to/repo2', array('shared' => true, 'bare' => true));
选项
  • shared (boolean) 指定 git 仓库将在几个用户之间共享
  • bare (boolean) 创建一个裸仓库

git log

$git->log(string $revRange = '', string $path = null, array $options = [])

返回提交日志

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$logs = $git->log(array('limit' => 10));
输出示例
[
    0 => [
        'hash'  => '1a821f3f8483747fd045eb1f5a31c3cc3063b02b',
        'name'  => 'John Doe',
        'email' => 'john@example.com',
        'date'  => 'Fri Jan 17 16:32:49 2014 +0900',
        'title' => 'Initial Commit'
    ],
    1 => [
        //...
    ]
]
选项
  • limit (integer) 限制要显示的提交数
  • skip (integer) 在开始显示提交输出之前跳过提交数

git merge

$git->merge(string|array|\Traversable $commit, string $message = null, array $options = [])

将指定提交的更改合并到当前分支

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->merge('1.0');
$git->merge('1.1', 'Merge message', ['strategy' => 'ours']);
选项
  • no-ff (boolean) 如果合并解析为快速前进,则不生成合并提交,仅更新分支指针
  • rerere-autoupdate (boolean) 如果可能,允许 rerere 机制使用自动冲突解决的结果更新索引
  • 压平 (布尔值) 允许您在当前分支上创建一个提交,其效果与合并另一个分支相同
  • 策略 (字符串) 使用指定的合并策略
  • 策略选项 (字符串) 通过合并策略传递合并策略特定选项

$git->merge->abort()

中止合并过程并尝试重建合并前的状态

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
try {
    $git->merge('dev');
} catch (PhpGit\Exception\GitException $e) {
    $git->merge->abort();
}

git mv

$git->mv(字符串|数组|\Iterator $source, 字符串 $destination, 数组 $options = [])

移动或重命名文件、目录或符号链接

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->mv('UPGRADE-1.0.md', 'UPGRADE-1.1.md');
选项
  • 强制 (布尔值) 强制重命名或移动文件,即使目标存在

git pull

$git->pull(字符串 $repository = null, 字符串 $refspec = null, 数组 $options = [])

从另一个仓库或本地分支获取并合并

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->pull('origin', 'master');

git push

$git->push(字符串 $repository = null, 字符串 $refspec = null, 数组 $options = [])

更新远程引用及其关联对象

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->push('origin', 'master');

git rebase

$git->rebase(字符串 $upstream = null, 字符串 $branch = null, 数组 $options = [])

将本地提交向前合并到更新的上游头

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->fetch('origin');
$git->rebase('origin/master');
选项
  • 起点 (字符串) 创建新提交的起始点
  • 不验证 (布尔值) 跳过预变基钩子
  • 强制变基 (布尔值) 强制变基,即使当前分支是您要变基的提交的后代分支

$git->rebase->continues()

在解决合并冲突后重新启动变基过程

$git->rebase->abort()

中止变基操作并将HEAD重置到原始分支

$git->rebase->skip()

跳过当前补丁重新启动变基过程

git remote

$git->remote()

返回现有远程的数组

$git = new PhpGit\Git();
$git->clone('https://github.com/kzykhys/Text.git', '/path/to/repo');
$git->setRepository('/path/to/repo');
$remotes = $git->remote();
输出示例
[
    'origin' => [
        'fetch' => 'https://github.com/kzykhys/Text.git',
        'push'  => 'https://github.com/kzykhys/Text.git'
    ]
]

$git->remote->add(字符串 $name, 字符串 $url, 数组 $options = [])

为存储在 $url 的仓库添加名为 $name 的远程

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->fetch('origin');
选项
  • 标签 (布尔值) 使用此选项,git fetch <name> 从远程仓库导入所有标签
  • 无标签 (布尔值) 使用此选项,git fetch <name> 不从远程仓库导入标签

$git->remote->rename(字符串 $name, 字符串 $newName)

将名为 $name 的远程重命名为 $newName

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->rename('origin', 'upstream');

$git->remote->rm(字符串 $name)

移除名为 $name 的远程

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->rm('origin');

$git->remote->show(字符串 $name)

提供有关远程 $name 的某些信息

$git = new PhpGit\Git();
$git->clone('https://github.com/kzykhys/Text.git', '/path/to/repo');
$git->setRepository('/path/to/repo');
echo $git->remote->show('origin');
输出示例
\* remote origin
  Fetch URL: https://github.com/kzykhys/Text.git
  Push  URL: https://github.com/kzykhys/Text.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

$git->remote->prune(字符串 $name = null)

删除 $name 下的所有过时的远程跟踪分支

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->prune('origin');

$git->remote->head(字符串 $name, 字符串 $branch = null)

等同于 set()

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->head('origin');

$git->remote->head->set(字符串 $name, 字符串 $branch)

设置指定远程的默认分支

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->head->set('origin');

$git->remote->head->delete(字符串 $name)

删除指定远程的默认分支

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->head->delete('origin');

$git->remote->head->remote(字符串 $name)

通过查询远程确定默认分支

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->head->remote('origin');

$git->remote->branches(字符串 $name, 数组 $branches)

等同于 set()

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->branches('origin', array('master', 'develop'));

$git->remote->branches->set(字符串 $name, 数组 $branches)

更改名为的远程跟踪的分支列表

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->branches->set('origin', array('master', 'develop'));

$git->remote->branches->add(字符串 $name, 数组 $branches)

将分支添加到名为的远程跟踪列表中

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->branches->add('origin', array('master', 'develop'));

$git->remote->url(字符串 $name, 字符串 $newUrl, 字符串 $oldUrl = null, 数组 $options = [])

等同于 set()

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->url('origin', 'https://github.com/text/Text.git');
选项
  • 推送 (布尔值) 操作推送URL而不是获取URL

$git->remote->url->set(字符串 $name, 字符串 $newUrl, 字符串 $oldUrl = null, 数组 $options = [])

将远程URL设置为 $newUrl

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->url->set('origin', 'https://github.com/text/Text.git');
选项
  • 推送 (布尔值) 操作推送URL而不是获取URL

$git->remote->url->add(字符串 $name, 字符串 $newUrl, 数组 $options = [])

将新URL添加到远程

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->url->add('origin', 'https://github.com/text/Text.git');
选项
  • 推送 (布尔值) 操作推送URL而不是获取URL

$git->remote->url->delete(字符串 $name, 字符串 $url, 数组 $options = [])

删除所有匹配正则表达式 $url 的 URL

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->url->delete('origin', 'https://github.com');
选项
  • 推送 (布尔值) 操作推送URL而不是获取URL

git 重置

$git->reset(string|array|\Traversable $paths, string $commit = null)

将所有 $paths 的索引条目重置为其在 $commit 时的状态

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->reset();

$git->reset->soft(string $commit = null)

将当前分支的头部重置为 $commit

完全不触碰索引文件和整个工作树(但将头部重置为 $commit,就像所有模式一样)。这会使所有更改的文件保持在“待提交的更改”状态,正如 git status 所展示的那样。

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->reset->soft();

$git->reset->mixed(string $commit = null)

将当前分支的头部重置为 $commit

重置索引但不影响工作树(即保留更改的文件,但不标记为提交)并报告未更新的内容。这是默认操作。

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->reset->mixed();

$git->reset->hard(string $commit = null)

将当前分支的头部重置为 $commit

重置索引和工作树。自 $commit 以来工作树中跟踪文件的任何更改都将被丢弃

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->reset->hard();

$git->reset->merge(string $commit = null)

将当前分支的头部重置为 $commit

重置索引并更新工作树中与 $commit 和 HEAD 不同的文件,但保留那些在索引和工作树之间不同的文件(即尚未添加的更改)。如果 $commit 和索引之间不同的文件有未暂存的更改,则重置操作被终止

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->reset->merge();

$git->reset->keep(string $commit = null)

将当前分支的头部重置为 $commit

重置索引并更新工作树中与 $commit 和 HEAD 不同的文件。如果 $commit 和 HEAD 之间不同的文件有本地更改,则重置操作被终止。

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->reset->keep();

$git->reset->mode(string $mode, string $commit = null)

将当前分支的头部重置为 $commit

根据 $mode 可能更新索引(将其重置为 $commit 的树)和工作树

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->reset->mode('hard');

git 删除

$git->rm(string|array|\Traversable $file, array $options = [])

从工作树和索引中删除文件

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->rm('CHANGELOG-1.0-1.1.txt', ['force' => true]);
选项
  • force (boolean) 覆盖最新的检查
  • cached (boolean) 仅从索引中取消暂存和删除路径
  • recursive (boolean) 当给出前缀目录名称时允许递归删除

$git->rm->cached(string|array|\Traversable $file, array $options = [])

等同于 $git->rm($file, ['cached' => true]);

选项
  • force (boolean) 覆盖最新的检查
  • recursive (boolean) 当给出前缀目录名称时允许递归删除

git 短日志

$git->shortlog(string|array|\Traversable $commits = HEAD)

总结 'git log' 输出

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$shortlog = $git->shortlog();
输出示例
[
    'John Doe <john@example.com>' => [
        0 => ['commit' => '589de67', 'date' => new \DateTime('2014-02-10 12:56:15 +0300'), 'subject' => 'Update README'],
        1 => ['commit' => '589de67', 'date' => new \DateTime('2014-02-15 12:56:15 +0300'), 'subject' => 'Update README'],
    ],
    //...
]

$git->shortlog->summary(string $commits = HEAD)

抑制提交描述,只提供提交计数摘要

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$shortlog = $git->shortlog->summary();
输出示例
[
    'John Doe <john@example.com>' => 153,
    //...
]

git 查看

$git->show(string $object, array $options = [])

显示一个或多个对象(blob、tree、tag 和提交)

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
echo $git->show('3ddee587e209661c8265d5bfd0df999836f6dfa2');
选项
  • format (string) 以指定格式美观地打印提交日志的内容,可以是 oneline、short、medium、full、fuller、email、raw 或 format 之一
  • abbrev-commit (boolean) 显示部分前缀而不是完整的 40 字节十六进制提交对象名称

git 保存工作现场

$git->stash()

将您的本地修改保存到一个新的暂存区,并运行 git reset --hard 来撤销它们

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->stash();

$git->stash->save(string $message = null, array $options = [])

将您的本地修改保存到一个新的暂存区,并运行 git reset --hard 来撤销它们。

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->stash->save('My stash');

$git->stash->lists(array $options = [])

返回您当前拥有的暂存区

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$stashes = $git->stash->lists();
输出示例
[
    0 => ['branch' => 'master', 'message' => '0e2f473 Fixes README.md'],
    1 => ['branch' => 'master', 'message' => 'ce1ddde Initial commit'],
]

$git->stash->show(string $stash = null)

以暂存状态与其原始父状态之间的差异形式显示暂存中记录的更改

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
echo $git->stash->show('stash@{0}');
输出示例
 REAMDE.md |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

$git->stash->drop(string $stash = null)

从暂存列表中删除单个暂存状态

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->stash->drop('stash@{0}');

$git->stash->pop(string $stash = null, array $options = [])

从暂存列表中删除单个暂存状态并将其应用于当前工作树状态之上

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->stash->pop('stash@{0}');

$git->stash->apply(string $stash = null, array $options = [])

与 pop 类似,但不从暂存列表中删除状态

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->stash->apply('stash@{0}');

$git->stash->branch(string $name, string $stash = null)

创建并检出从原始创建提交开始的新的分支,将记录在新的工作树和索引中的更改应用

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->stash->branch('hotfix', 'stash@{0}');

$git->stash->clear()

删除所有存储的状态

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->stash->clear();

$git->stash->create()

创建一个存储(这是一个常规的提交对象)并返回其对象名称,不将其存储在任何引用命名空间中

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$commit = $git->stash->create();
输出示例
877316ea6f95c43b7ccc2c2a362eeedfa78b597d

git status

$git->status(array $options = [])

返回工作树状态

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$status = $git->status();
常量
  • StatusCommand::UNMODIFIED [=' '] 未修改
  • StatusCommand::MODIFIED [='M'] 已修改
  • StatusCommand::ADDED [='A'] 已添加
  • StatusCommand::DELETED [='D'] 已删除
  • StatusCommand::RENAMED [='R'] 已重命名
  • StatusCommand::COPIED [='C'] 已复制
  • StatusCommand::UPDATED_BUT_UNMERGED [='U'] 已更新但未合并
  • StatusCommand::UNTRACKED [='?'] 未跟踪
  • StatusCommand::IGNORED [='!'] 已忽略
输出示例
[
    'branch' => 'master',
    'changes' => [
        ['file' => 'item1.txt', 'index' => 'A', 'work_tree' => 'M'],
        ['file' => 'item2.txt', 'index' => 'A', 'work_tree' => ' '],
        ['file' => 'item3.txt', 'index' => '?', 'work_tree' => '?'],
    ]
]
选项
  • ignored (boolean) 同时显示忽略的文件

git tag

$git->tag()

返回一个标签数组

$git = new PhpGit\Git();
$git->clone('https://github.com/kzykhys/PhpGit.git', '/path/to/repo');
$git->setRepository('/path/to/repo');
$tags = $git->tag();
输出示例
['v1.0.0', 'v1.0.1', 'v1.0.2']

$git->tag->create(string $tag, string $commit = null, array $options = [])

创建一个标签对象

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->tag->create('v1.0.0');
选项
  • annotate (boolean) 创建一个未签名的注释标签对象
  • sign (boolean) 使用默认电子邮件地址的密钥创建GPG签名的标签
  • force (boolean) 用给定的名称替换现有标签(而不是失败)

$git->tag->delete(string|array|\Traversable $tag)

删除具有给定名称的现有标签

$git->tag->verify(string|array|\Traversable $tag)

验证给定标签名的GPG签名

git ls-tree

$git->tree(string $branch = master, string $path = '')

返回树对象的内容

$git = new PhpGit\Git();
$git->clone('https://github.com/kzykhys/PhpGit.git', '/path/to/repo');
$git->setRepository('/path/to/repo');
$tree = $git->tree('master');
输出示例
[
    ['mode' => '100644', 'type' => 'blob', 'hash' => '1f100ce9855b66111d34b9807e47a73a9e7359f3', 'file' => '.gitignore', 'sort' => '2:.gitignore'],
    ['mode' => '100644', 'type' => 'blob', 'hash' => 'e0bfe494537037451b09c32636c8c2c9795c05c0', 'file' => '.travis.yml', 'sort' => '2:.travis.yml'],
    ['mode' => '040000', 'type' => 'tree', 'hash' => '8d5438e79f77cd72de80c49a413f4edde1f3e291', 'file' => 'bin', 'sort' => '1:.bin'],
]

许可证

MIT许可证

作者

由Kazuyuki Hayashi (@kzykhys) 开发,由 @inhere 维护