kzykhys/git

PHP5.3+ 的 Git 封装器

v0.1.2 2014-02-15 06:18 UTC

This package is not auto-updated.

Last update: 2024-09-10 01:58:31 UTC


README

Latest Unstable Version Build Status Coverage Status SensioLabsInsight

要求

  • PHP5.3
  • Git

安装

更新 composer.json 并运行 composer update

{
    "require": {
        "kzykhys/git": "dev-master"
    }
}

基本用法

<?php

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

$git = new PHPGit\Git();
$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']);
    }
}

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(数组 $options = [])
  • git init
    • $git->init(字符串 $path, 数组 $options = [])
  • git log
    • $git->log(字符串 $revRange = '', 字符串 $path = null, 数组 $options = [])
  • git merge
    • $git->merge(字符串|数组|\Traversable $commit, 字符串 $message = null, 数组 $options = [])
    • $git->merge->abort()
  • git mv
    • $git->mv(字符串|数组|\Iterator $source, 字符串 $destination, 数组 $options = [])
  • git pull
    • $git->pull(字符串 $repository = null, 字符串 $refspec = null, 数组 $options = [])
  • git push
    • $git->push(字符串 $repository = null, 字符串 $refspec = null, 数组 $options = [])
  • git rebase
    • $git->rebase(字符串 $upstream = null, 字符串 $branch = null, 数组 $options = [])
    • $git->rebase->continues()
    • $git->rebase->abort()
    • $git->rebase->skip()
  • git remote
    • $git->remote()
    • $git->remote->add(字符串 $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(字符串|array|\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(字符串|array|\Traversable $file, 数组 $options = [])
    • $git->rm->cached(字符串|array|\Traversable $file, 数组 $options = [])
  • git shortlog
    • $git->shortlog(字符串|array|\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(字符串|数组|\Traversable $tag)
  • git ls-tree
    • $git->tree(字符串 $branch = master, 字符串 $path = '')

git add

$git->add(字符串|数组|\Traversable $file, 数组 $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, 字符串|数组|\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 (布尔值) 在存档中的每个文件名前添加前缀/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(字符串 $branch, 字符串 $startPoint = null, 数组 $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 (布尔值) 如果 $branch 已存在,则重置 $branch$startPoint

$git->branch->move(字符串 $branch, 字符串 $newBranch, 数组 $options = [])

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

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

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

删除分支

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

分支必须完全合并到其上游分支,或者在未设置 --track 或 --set-upstream 的情况下合并到 HEAD。

选项
  • force (布尔值) 不考虑合并状态删除分支

git cat-file

$git->cat->blob(字符串 $object)

返回 blob 对象的内容

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

$git->cat->type(字符串 $object)

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

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

$git->cat->size(字符串 $object)

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

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

git checkout

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

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

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

$git->checkout->create(字符串 $branch, 字符串 $startPoint = null, 数组 $options = [])

创建新分支并检出

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

$git->checkout->orphan(字符串 $branch, 字符串 $startPoint = null, 数组 $options = [])

创建一个新的孤儿分支,命名为

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

git clone

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

将仓库克隆到新目录

$git = new PHPGit\Git();
$git->clone('https://github.com/kzykhys/PHPGit.git', '/path/to/repo');
选项
  • shared (布尔值) 开始时不包含任何自己的对象
  • bare (布尔值) 创建裸 Git 仓库

git commit

$git->commit(字符串 $message, 数组 $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 (布尔值) 阶段已修改和已删除的文件
  • reuse-message (字符串) 从现有的提交对象中获取,并在创建提交时重用日志消息和作者信息(包括时间戳)
  • squash (字符串) 为 rebase --autosquash 构建提交消息
  • author (字符串) 覆盖提交作者
  • date (字符串) 覆盖提交中使用的作者日期
  • cleanup (字符串) 可以是 verbatim、whitespace、strip 或 default
  • amend (布尔值) 用于修改当前分支的尖端

git config

$git->config(数组 $options = [])

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

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

$git->config->set(字符串 $name, 字符串 $value, 数组 $options = [])

设置选项

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

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

向选项中添加新行而不修改任何现有值

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

git describe

$git->describe(字符串 $committish = null, 数组 $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 (布尔值) 启用匹配任何已知分支、远程跟踪分支或轻量级标签
  • tags (布尔值) 启用匹配轻量级(非注解)标签
  • always (布尔值) 以唯一缩写的提交对象作为后备显示

$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(字符串|array|\Iterator $source, 字符串 $destination, 数组 $options = [])

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

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

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');
选项
  • onto (字符串) 创建新提交的起点
  • no-verify (布尔型) 跳过 pre-rebase 插件
  • force-rebase (布尔型) 强制 rebase,即使当前分支是你要 rebase 的提交的子分支

$git->rebase->continues()

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

$git->rebase->abort()

终止 rebase 操作并将 HEAD 重置到原始分支

$git->rebase->skip()

通过跳过当前补丁重新启动 rebase 过程

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');
选项
  • tags (布尔型) 使用此选项,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 URL 而不是 fetch 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');
选项
  • push (布尔型) 操作 push URL 而不是 fetch 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');
选项
  • push (布尔型) 操作 push URL 而不是 fetch 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');
选项
  • push (布尔型) 操作 push URL 而不是 fetch URL

git reset

$git->reset(字符串|数组|\Traversable $paths, 字符串 $commit = null)

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

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

$git->reset->soft(字符串 $commit = null)

将当前分支头重置为 $commit

完全不触及索引文件和工作树(但重置 head 到 $commit,就像所有模式一样)。这会保留所有已更改的文件为“待提交的更改”,正如 git status 会显示的那样。

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

$git->reset->mixed(字符串 $commit = null)

将当前分支头重置为 $commit

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

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

$git->reset->hard(字符串 $commit = null)

将当前分支头重置为 $commit

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

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

$git->reset->merge(字符串 $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)

创建并检出一个新的名为 $name 的分支,从最初创建的提交开始,将记录的更改应用到新工作树和索引中

$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(字符串 $tag, 字符串 $commit = null, 数组 $options = [])

创建标签对象

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

删除现有标签:$git->tag->delete(字符串|array|\Traversable $tag)

删除给定名称的现有标签

验证标签:$git->tag->verify(字符串|array|\Traversable $tag)

验证给定标签名的GPG签名

git ls-tree

获取树对象内容:$git->tree(字符串 $branch = master, 字符串 $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许可证

作者

Hayashi Kazuyuki (@kzykhys)