phpcom-lab/phpgit

此包已被弃用,不再维护。作者建议使用 phppkg/phpgit 包。

PHP 7.1+ 的 Git 包装器

v0.2.4 2021-08-18 08:14 UTC

README

GitHub tag (latest SemVer) Github Actions Status Php Version

PhpGit - PHP 7.1+ 的 Git 包装器

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

要求

  • PHP 7.1+
  • Git

安装

更新您的 composer.json 并运行 composer update

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

基本用法

<?php

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

$git = PhpGit\Git::new();
$git->clone('https://github.com/kzykhys/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($logFmt);
$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;

将会看到

> 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->创建(字符串 $branch, 字符串 $startPoint = null, 数组 $options = [])
    • $git->checkout->孤儿(字符串 $branch, 字符串 $startPoint = null, 数组 $options = [])
  • git克隆
    • $git->克隆(字符串 $repository, 字符串 $path = null, 数组 $options = [])
  • git提交
    • $git->提交(字符串 $message, 数组 $options = [])
  • git配置
    • $git->配置(数组 $options = [])
    • $git->config->设置(字符串 $name, 字符串 $value, 数组 $options = [])
    • $git->config->添加(字符串 $name, 字符串 $value, 数组 $options = [])
  • git描述
    • $git->描述(字符串 $committish = null, 数组 $options = [])
    • $git->describe->标签(字符串 $committish = null, 数组 $options = [])
  • git获取
    • $git->获取(字符串 $repository, 字符串 $refspec = null, 数组 $options = [])
    • $git->fetch->全部(数组 $options = [])
  • git初始化
    • $git->初始化(字符串 $path, 数组 $options = [])
  • git日志
    • $git->日志(字符串 $revRange = '', 字符串 $path = null, 数组 $options = [])
  • git合并
    • $git->合并(字符串|array|\Traversable $commit, 字符串 $message = null, 数组 $options = [])
    • $git->merge->中止()
  • git移动
    • $git->移动(字符串|array|\Iterator $source, 字符串 $destination, 数组 $options = [])
  • git拉取
    • $git->拉取(字符串 $repository = null, 字符串 $refspec = null, 数组 $options = [])
  • git推送
    • $git->推送(字符串 $repository = null, 字符串 $refspec = null, 数组 $options = [])
  • git变基
    • $git->变基(字符串 $upstream = null, 字符串 $branch = null, 数组 $options = [])
    • $git->rebase->继续()
    • $git->rebase->中止()
    • $git->rebase->跳过()
  • git远程
    • $git->远程()
    • $git->remote->添加(字符串 $name, 字符串 $url, 数组 $options = [])
    • $git->remote->rename(字符串 $name, 字符串 $newName)
    • $git->remote->rm(字符串 $name)
    • $git->remote->show(字符串 $name)
    • $git->remote->prune(字符串 $name = null)
    • $git->remote->head(字符串 $name, 字符串 $branch = null)
    • $git->remote->head->set(字符串 $name, 字符串 $branch)
    • $git->remote->head->delete(字符串 $name)
    • $git->remote->head->remote(字符串 $name)
    • $git->remote->branches(字符串 $name, 数组 $branches)
    • $git->remote->branches->set(字符串 $name, 数组 $branches)
    • $git->remote->branches->add(字符串 $name, 数组 $branches)
    • $git->remote->url(字符串 $name, 字符串 $newUrl, 字符串 $oldUrl = null, 数组 $options = [])
    • $git->remote->url->set(字符串 $name, 字符串 $newUrl, 字符串 $oldUrl = null, 数组 $options = [])
    • $git->remote->url->add(字符串 $name, 字符串 $newUrl, 数组 $options = [])
    • $git->remote->url->delete(字符串 $name, 字符串 $url, 数组 $options = [])
  • git reset
    • $git->reset(字符串|数组|\Traversable $paths, 字符串 $commit = null)
    • $git->reset->soft(字符串 $commit = null)
    • $git->reset->mixed(字符串 $commit = null)
    • $git->reset->hard(字符串 $commit = null)
    • $git->reset->merge(字符串 $commit = null)
    • $git->reset->keep(字符串 $commit = null)
    • $git->reset->mode(字符串 $mode, 字符串 $commit = null)
  • git rm
    • $git->rm(字符串|数组|\Traversable $file, 数组 $options = [])
    • $git->rm->cached(字符串|数组|\Traversable $file, 数组 $options = [])
  • git shortlog
    • $git->shortlog(字符串|数组|\Traversable $commits = HEAD)
    • $git->shortlog->summary(字符串 $commits = HEAD)
  • git show
    • $git->show(字符串 $object, 数组 $options = [])
  • git stash
    • $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(字符串 $stash = null, 数组 $options = [])
    • $git->stash->branch(字符串 $name, 字符串 $stash = null)
    • $git->stash->clear()
    • $git->stash->create()
  • git status
    • $git->status(数组 $options = [])
  • git tag
    • $git->tag()
    • $git->tag->create(字符串 $tag, 字符串 $commit = null, 数组 $options = [])
    • $git->tag->delete(字符串|array|\Traversable $tag)
    • $git->tag->verify(字符串|array|\Traversable $tag)
  • git ls-tree
    • $git->tree(字符串 $branch = master, 字符串 $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 (布尔值) 允许添加否则被忽略的文件
  • ignore-errors (布尔值) 不终止操作

git archive

$git->archive(字符串 $file, 字符串 $tree = null, 字符串|array|\Traversable $path = null, 数组 $options = [])

从命名树创建文件存档

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

git branch

$git->branch(数组 $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 (布尔值) 列出远程跟踪分支和本地分支
  • remotes (布尔值) 列出远程跟踪分支

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

创建一个名为 $branch 的新分支头,指向当前 HEAD,或如果提供,则指向 $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 已经存在,则将其重置为 $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) 启用匹配轻量级(非注解)标签
  • 总是 (布尔值) 以唯一缩写的提交对象作为后备显示

$git->describe->tags(字符串 $committish = null, 数组 $options = [])

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

git fetch

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

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

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

$git->fetch->all(数组 $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 (布尔值) 将获取的引用名称和对象名称追加到 .git/FETCH_HEAD 的现有内容中
  • keep (布尔值) 保留下载的包
  • prune (布尔值) 在获取后,删除任何远程跟踪分支,这些分支在远程不再存在

git init

$git->init(字符串 $path, 数组 $options = [])

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

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

git log

$git->log(字符串 $revRange = '', 字符串 $path = null, 数组 $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 (整数) 限制显示的提交数量
  • skip (整数) 在开始显示提交输出之前跳过指定数量的提交

git merge

$git->merge(字符串|array|\Traversable $commit, 字符串 $message = null, 数组 $options = [])

将命名的提交的更改合并到当前分支中

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

$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(string|array|\Iterator $source, string $destination, array $options = [])

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

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

git pull

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

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

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

git push

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

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

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

git rebase

$git->rebase(string $upstream = null, string $branch = null, array $options = [])

将本地提交向前移植到更新的上游头

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->fetch('origin');
$git->rebase('origin/master');
选项
  • onto (string) 创建新提交的起始点
  • no-verify (boolean) 跳过预变基钩子
  • force-rebase (boolean) 即使当前分支是你将要变基到的提交的后代,也强制变基

$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(string $name, string $url, array $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');
选项
  • tags (boolean) 使用此选项,git fetch <name> 从远程仓库导入所有标签
  • no-tags (布尔值) 使用此选项,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');
选项
  • push (布尔值) 操作push URLs而不是fetch URLs

$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');
选项
  • push (布尔值) 操作push URLs而不是fetch URLs

$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');
选项
  • push (布尔值) 操作push URLs而不是fetch URLs

$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');
选项
  • push (布尔值) 操作push URLs而不是fetch URLs

git reset

$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 rm

$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 shortlog

$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 show

$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 stash

$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()

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

$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 维护