Access git repositories from PHP 8.4+, through the git binary or libgit2.
use Gitonomy\Git\Repository;
$repository = new Repository('/path/to/repository');
// The last 5 commits of main that changed src/
foreach ($repository->getLog('main', 'src', limit: 5) as $commit) {
echo $commit->getShortHash().' '.$commit->getSubjectMessage().PHP_EOL;
}
// The files the last commit changed
foreach ($repository->getHeadCommit()->getDiff()->getFiles() as $file) {
echo $file->getName().': +'.$file->getAdditions().' -'.$file->getDeletions().PHP_EOL;
}
// Who wrote the first lines of README.md
foreach ($repository->getBlame('main', 'README.md', 1, 3)->getLines() as $line) {
echo $line->getCommit()->getAuthorName().': '.$line->getContent().PHP_EOL;
}The documentation is at gitonomy.github.io/gitlib.
- Open a bare repository, a working tree, a linked worktree or a submodule, or create one: init, clone, mirror. See Opening a repository and Creating repositories.
- Read commits, logs, trees, blobs, diffs, blames, branches and tags, as PHP objects.
- Change the working copy: checkout, new branches, merges, and the diffs of what is pending or staged. See Working copy.
- Manage the hooks of a repository: see Hooks.
- Run any other git command: see Running git commands.
Repository runs git through a driver, its last argument:
CliDriver, the default, runs thegitbinary, 1.8.4 or later, and needs nothing else;LibGit2Driver, experimental, runs libgit2 1.9 in-process, through PHP'sFFIextension. It is faster for many small reads in a long-running process, and slower on long histories, like blames: see "Choosing a driver".
use Gitonomy\Git\Driver\Ffi\LibGit2Driver;
use Gitonomy\Git\Repository;
$repository = new Repository('/path/to/repository', driver: new LibGit2Driver());composer require gitonomy/gitlibUpgrading from 1.x? Read UPGRADE-2.0.md and the changelog.
- Opening a repository: paths, drivers, errors, git configuration
- Creating repositories
- Revisions
- Commits
- Log
- Trees
- Blobs
- Diffs
- Blame
- Tags and branches
- Branches
- Working copy
- Hooks
Available as part of the Tidelift Subscription
The maintainers of gitonomy/gitlib and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.
gitlib is released under the MIT license.