Library for work with Git repository in PHP.

Fund package maintenance!

Installs: 2 670 736

Dependents: 69

Suggesters: 1

Security: 1

Stars: 487

Watchers: 24

Forks: 101

Open Issues: 17

v4.2.0 2023-07-12 09:14 UTC


Build Status Downloads this Month Latest Stable Version License

Library for work with Git repository in PHP.



Download a latest package or use Composer:

composer require czproject/git-php

Library requires PHP 5.6 or later and git client (path to Git must be in system variable PATH).

Git installers:


$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
	sit amet

// commit
$repo->commit('init commit');

Initialization of empty repository

$repo = $git->init('/path/to/repo-directory');

With parameters:

$repo = $git->init('/path/to/repo-directory', [
	'--bare', // creates bare repo

Cloning of repository

// Cloning of repository into subdirectory 'git-php' in current working directory
$repo = $git->cloneRepository('');

// Cloning of repository into own directory
$repo = $git->cloneRepository('', '/path/to/my/subdir');

Basic operations

$repo->hasChanges();    // returns boolean
$repo->commit('commit message');


// adds files into commit
$repo->addFile('file1.txt', 'file2.txt');
$repo->addFile(['file3.txt', 'file4.txt']);

// renames files in repository
$repo->renameFile('old.txt', 'new.txt');
    'old1.txt' => 'new1.txt',
    'old2.txt' => 'new2.txt',

// removes files from repository
$repo->removeFile('file1.txt', 'file2.txt');
$repo->removeFile(['file3.txt', 'file4.txt']);

// adds all changes in repository


// gets list of all repository branches (remotes & locals)

// gets list of all local branches

// gets name of current branch

// creates new branch

// creates new branch and checkout
$repo->createBranch('patch-1', TRUE);

// removes branch


// gets list of all tags in repository

// creates new tag
$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


// 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

// returns commit data of last commit on current branch
$commit = $repo->getLastCommit();


// pulls changes from remote
$repo->pull('remote-name', ['--options']);

// pushs changes to remote
$repo->push('remote-name', ['--options']);
$repo->push(['origin', 'master'], ['-u']);

// fetchs changes from remote
$repo->fetch('remote-name', ['--options']);
$repo->fetch(['origin', 'master']);

// adds remote repository
$repo->addRemote('remote-name', 'repository-url', ['--options']);
$repo->addRemote('origin', '');

// renames remote
$repo->renameRemote('old-remote-name', 'new-remote-name');
$repo->renameRemote('origin', 'upstream');

// removes remote

// changes remote URL
$repo->setRemoteUrl('remote-name', 'new-repository-url');
$repo->setRemoteUrl('upstream', '');

Troubleshooting - How to provide username and password for commands

  1. use SSH instead of HTTPS -
  2. store credentials to Git Credential Storage
  3. insert user and password into remote URL -
    • git remote add origin https://user:password@server/path/repo.git
  4. for push() you can use --repo argument -
    • $git->push(NULL, ['--repo' => 'https://user:password@server/path/repo.git']);

Other commands

For running other commands you can use execute method:

$output = $repo->execute('command');
$output = $repo->execute('command', 'with', 'parameters');

// example:
$repo->execute('remote', 'set-branches', $originName, $branches);

Custom methods

You can create custom methods. For example:

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', [

License: New BSD License
Author: Jan Pecha,