PHPackages                             czproject/git-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. czproject/git-php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

czproject/git-php
=================

Library for work with Git repository in PHP.

v4.6.0(7mo ago)5645.1M↓28.7%108[6 issues](https://github.com/czproject/git-php/issues)[13 PRs](https://github.com/czproject/git-php/pulls)20BSD-3-ClausePHPPHP 8.0 - 8.5CI passing

Since Apr 19Pushed 7mo ago20 watchersCompare

[ Source](https://github.com/czproject/git-php)[ Packagist](https://packagist.org/packages/czproject/git-php)[ GitHub Sponsors](https://github.com/sponsors/janpecha)[ Fund](https://www.janpecha.cz/donate/git-php/)[ RSS](/packages/czproject-git-php/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (1)Versions (48)Used By (20)Security (1)

Git-PHP
=======

[](#git-php)

[![Build Status](https://github.com/czproject/git-php/workflows/Build/badge.svg)](https://github.com/czproject/git-php/actions)[![Downloads this Month](https://camo.githubusercontent.com/213e5b398f7ba9427707f76e87a2f0c3c436120516b9a8e3f3da0b182dfdcde7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f637a70726f6a6563742f6769742d7068702e737667)](https://packagist.org/packages/czproject/git-php)[![Latest Stable Version](https://camo.githubusercontent.com/b50b690ec39caa22fb17e38f6df7f13a4c20f6272724072d6d0e6563508a43c2/68747470733a2f2f706f7365722e707567782e6f72672f637a70726f6a6563742f6769742d7068702f762f737461626c65)](https://github.com/czproject/git-php/releases)[![License](https://camo.githubusercontent.com/fa7d5fcf2c84b580327af52da95dd751703af65f079dc3c5a0081beac0789718/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4e65772532304253442d626c75652e737667)](https://github.com/czproject/git-php/blob/master/license.md)

Library for work with Git repository in PHP.

[![Donate](https://camo.githubusercontent.com/101b981194f1dafbf9c42e19c3034fe2d724e75be972cef0f4477074997834db/68747470733a2f2f6275796d65636f666665652e696e746d2e6f72672f696d672f646f6e6174652d62616e6e65722e76312e737667)](https://www.janpecha.cz/donate/git-php/)

Tip

You can use [GitHub Sponsors](https://github.com/sponsors/janpecha), [Stripe.com](https://donate.stripe.com/7sIcO2a9maTSg2A9AA), [Thanks.dev](https://thanks.dev/u/gh/czproject) or [other](https://www.janpecha.cz/donate/git-php/) method.

Installation
------------

[](#installation)

[Download a latest package](https://github.com/czproject/git-php/releases) or use [Composer](http://getcomposer.org/):

```
composer require czproject/git-php

```

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

Git installers:

- for Linux -
- for Windows -
- for others -

Usage
-----

[](#usage)

```
$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');
```

Initialization of empty repository
----------------------------------

[](#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)

```
// 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');
```

Basic operations
----------------

[](#basic-operations)

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

Branches
--------

[](#branches)

```
// 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');
```

Tags
----

[](#tags)

```
// 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');
```

History
-------

[](#history)

```
// 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();
```

Remotes
-------

[](#remotes)

```
// 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', 'git@github.com: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');
```

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

[](#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
--------------

[](#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', [
	'branch-1',
	'branch-2',
]);
```

---

License: [New BSD License](license.md)
Author: Jan Pecha,

###  Health Score

71

—

ExcellentBetter than 100% of packages

Maintenance65

Regular maintenance activity

Popularity66

Solid adoption and visibility

Community44

Growing community involvement

Maturity93

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 92.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~106 days

Recently: every ~213 days

Total

44

Last Release

211d ago

Major Versions

v1.0.2 → v2.0.02013-08-18

v2.0.0 → v3.0.02013-09-22

v3.18.2 → v4.0.02021-04-29

PHP version history (6 changes)v2.0.0PHP &gt;=5.3.0

v3.1.0PHP &gt;=5.4.0

v4.0.0PHP &gt;=5.6.0

v4.4.0PHP &gt;=7.4.0

v4.5.0PHP 8.0 - 8.4

v4.6.0PHP 8.0 - 8.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/5c980b1511b4a0350442dc23d89c99d4d9a2411b7e765d52c133ccacf616968b?d=identicon)[janpecha](/maintainers/janpecha)

---

Top Contributors

[![janpecha](https://avatars.githubusercontent.com/u/637719?v=4)](https://github.com/janpecha "janpecha (165 commits)")[![sudo-plz](https://avatars.githubusercontent.com/u/5683136?v=4)](https://github.com/sudo-plz "sudo-plz (2 commits)")[![ribeiropaulor](https://avatars.githubusercontent.com/u/376830?v=4)](https://github.com/ribeiropaulor "ribeiropaulor (2 commits)")[![c33s](https://avatars.githubusercontent.com/u/649209?v=4)](https://github.com/c33s "c33s (1 commits)")[![Damienbelingheri](https://avatars.githubusercontent.com/u/59914068?v=4)](https://github.com/Damienbelingheri "Damienbelingheri (1 commits)")[![gerryd](https://avatars.githubusercontent.com/u/3003371?v=4)](https://github.com/gerryd "gerryd (1 commits)")[![Bert-Oja](https://avatars.githubusercontent.com/u/16765429?v=4)](https://github.com/Bert-Oja "Bert-Oja (1 commits)")[![Justin991q](https://avatars.githubusercontent.com/u/4533867?v=4)](https://github.com/Justin991q "Justin991q (1 commits)")[![kawax](https://avatars.githubusercontent.com/u/1502086?v=4)](https://github.com/kawax "kawax (1 commits)")[![kernel23](https://avatars.githubusercontent.com/u/76821636?v=4)](https://github.com/kernel23 "kernel23 (1 commits)")[![mawalu](https://avatars.githubusercontent.com/u/2027387?v=4)](https://github.com/mawalu "mawalu (1 commits)")[![Asenar](https://avatars.githubusercontent.com/u/1180301?v=4)](https://github.com/Asenar "Asenar (1 commits)")[![Bird87ZA](https://avatars.githubusercontent.com/u/1457444?v=4)](https://github.com/Bird87ZA "Bird87ZA (1 commits)")

---

Tags

gitgit-phpgit-wrapperphpgit

### Embed Badge

![Health badge](/badges/czproject-git-php/health.svg)

```
[![Health](https://phpackages.com/badges/czproject-git-php/health.svg)](https://phpackages.com/packages/czproject-git-php)
```

###  Alternatives

[cypresslab/gitelephant

An abstraction layer for git written in PHP

6181.1M27](/packages/cypresslab-gitelephant)[teqneers/php-stream-wrapper-for-git

Git Stream Wrapper for PHP

2862.2M8](/packages/teqneers-php-stream-wrapper-for-git)[bruli/php-git-hooks

Git hooks for PHP projects.

674371.6k5](/packages/bruli-php-git-hooks)[marcocesarato/php-conventional-changelog

Generate changelogs and release notes from a project's commit messages and metadata and automate versioning with semver.org and conventionalcommits.org

2521.4M117](/packages/marcocesarato-php-conventional-changelog)[ramsey/conventional-commits

A PHP library for creating and validating commit messages according to the Conventional Commits specification. Includes a CaptainHook action!

1931.5M142](/packages/ramsey-conventional-commits)[sebastianfeldmann/git

PHP git wrapper

487.4M20](/packages/sebastianfeldmann-git)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
