czproject / git-php
用于在 PHP 中操作 Git 仓库的库。
v4.3.0
2024-08-05 13:28 UTC
Requires
- php: >=5.6.0
Requires (Dev)
- nette/tester: ^2.0
- dev-master
- v4.3.0
- v4.2.0
- v4.1.0
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v3.18.2
- v3.18.1
- v3.18.0
- v3.17.1
- v3.17.0
- v3.16.2
- v3.16.1
- v3.16.0
- v3.15.1
- v3.15.0
- v3.14.0
- v3.13.1
- v3.13.0
- v3.12.0
- v3.11.0
- 3.10.0
- v3.9.1
- v3.9.0
- v3.9.0-RC1
- v3.8.0
- v3.7.0
- v3.6.0
- v3.5.0
- v3.4.0
- v3.3.0
- v3.2.0
- v3.1.0
- v3.0.0
- v2.0.0
- v1.0.2
- v1.0.1
- v1.0.0
- dev-version-4.x
- dev-wip-is-remote-readable
- dev-version-2
This package is auto-updated.
Last update: 2024-09-18 16:11:47 UTC
README
用于在 PHP 中操作 Git 仓库的库。
安装
composer require czproject/git-php
库需要 PHP 5.6 或更高版本和 git
客户端(Git 的路径必须位于系统变量 PATH
中)。
Git 安装程序
- Linux - https://git.js.cn/download/linux
- Windows - https://git.js.cn/download/win
- 其他 - https://git.js.cn/downloads
使用方法
$git = new CzProject\GitPhp\Git; // create repo object $repo = $git->open('/path/to/repo'); // create a new file in repo $filename = $repo->getRepositoryPath() . '/readme.txt'; file_put_contents($filename, "Lorem ipsum dolor sit amet "); // commit $repo->addFile($filename); $repo->commit('init commit');
初始化空仓库
$repo = $git->init('/path/to/repo-directory');
带参数
$repo = $git->init('/path/to/repo-directory', [ '--bare', // creates bare repo ]);
克隆仓库
// Cloning of repository into subdirectory 'git-php' in current working directory $repo = $git->cloneRepository('https://github.com/czproject/git-php.git'); // Cloning of repository into own directory $repo = $git->cloneRepository('https://github.com/czproject/git-php.git', '/path/to/my/subdir');
基本操作
$repo->hasChanges(); // returns boolean $repo->commit('commit message'); $repo->merge('branch-name'); $repo->checkout('master'); $repo->getRepositoryPath(); // adds files into commit $repo->addFile('file.txt'); $repo->addFile('file1.txt', 'file2.txt'); $repo->addFile(['file3.txt', 'file4.txt']); // renames files in repository $repo->renameFile('old.txt', 'new.txt'); $repo->renameFile([ 'old1.txt' => 'new1.txt', 'old2.txt' => 'new2.txt', ]); // removes files from repository $repo->removeFile('file.txt'); $repo->removeFile('file1.txt', 'file2.txt'); $repo->removeFile(['file3.txt', 'file4.txt']); // adds all changes in repository $repo->addAllChanges();
分支
// gets list of all repository branches (remotes & locals) $repo->getBranches(); // gets list of all local branches $repo->getLocalBranches(); // gets name of current branch $repo->getCurrentBranchName(); // creates new branch $repo->createBranch('new-branch'); // creates new branch and checkout $repo->createBranch('patch-1', TRUE); // removes branch $repo->removeBranch('branch-name');
标签
// gets list of all tags in repository $repo->getTags(); // creates new tag $repo->createTag('v1.0.0'); $repo->createTag('v1.0.0', $options); $repo->createTag('v1.0.0', [ '-m' => 'message', ]); // renames tag $repo->renameTag('old-tag-name', 'new-tag-name'); // removes tag $repo->removeTag('tag-name');
历史记录
// returns last commit ID on current branch $commitId = $repo->getLastCommitId(); $commitId->getId(); // or (string) $commitId // returns commit data $commit = $repo->getCommit('734713bc047d87bf7eac9674765ae793478c50d3'); $commit->getId(); // instance of CommitId $commit->getSubject(); $commit->getBody(); $commit->getAuthorName(); $commit->getAuthorEmail(); $commit->getAuthorDate(); $commit->getCommitterName(); $commit->getCommitterEmail(); $commit->getCommitterDate(); $commit->getDate(); // returns commit data of last commit on current branch $commit = $repo->getLastCommit();
远程
// pulls changes from remote $repo->pull('remote-name', ['--options']); $repo->pull('origin'); // pushs changes to remote $repo->push('remote-name', ['--options']); $repo->push('origin'); $repo->push(['origin', 'master'], ['-u']); // fetchs changes from remote $repo->fetch('remote-name', ['--options']); $repo->fetch('origin'); $repo->fetch(['origin', 'master']); // adds remote repository $repo->addRemote('remote-name', 'repository-url', ['--options']); $repo->addRemote('origin', '[email protected]:czproject/git-php.git'); // renames remote $repo->renameRemote('old-remote-name', 'new-remote-name'); $repo->renameRemote('origin', 'upstream'); // removes remote $repo->removeRemote('remote-name'); $repo->removeRemote('origin'); // changes remote URL $repo->setRemoteUrl('remote-name', 'new-repository-url'); $repo->setRemoteUrl('upstream', 'https://github.com/czproject/git-php.git');
故障排除 - 如何为命令提供用户名和密码
- 使用 SSH 而不是 HTTPS - https://stackoverflow.com/a/8588786
- 将凭据存储到 Git 凭据存储
- 将用户名和密码插入远程 URL - https://stackoverflow.com/a/16381160
git remote add origin https://user:password@server/path/repo.git
- 对于
push()
,您可以使用--repo
参数 - https://stackoverflow.com/a/12193555$git->push(NULL, ['--repo' => 'https://user:password@server/path/repo.git']);
其他命令
要运行其他命令,您可以使用 execute
方法
$output = $repo->execute('command'); $output = $repo->execute('command', 'with', 'parameters'); // example: $repo->execute('remote', 'set-branches', $originName, $branches);
自定义方法
您可以创建自定义方法。例如
class OwnGit extends \CzProject\GitPhp\Git { public function open($directory) { return new OwnGitRepository($directory, $this->runner); } } class OwnGitRepository extends \CzProject\GitPhp\GitRepository { public function setRemoteBranches($name, array $branches) { $this->run('remote', 'set-branches', $name, $branches); return $this; } } $git = new OwnGit; $repo = $git->open('/path/to/repo'); $repo->addRemote('origin', 'repository-url'); $repo->setRemoteBranches('origin', [ 'branch-1', 'branch-2', ]);
许可协议: 新 BSD 许可协议
作者: Jan Pecha, https://www.janpecha.cz/