PHPackages                             penobit/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. penobit/git-php

ActiveLibrary

penobit/git-php
===============

Library for work with Git repository in PHP.

v1.1(4y ago)325BSD-3-ClausePHPPHP &gt;=5.6.0

Since Jun 1Pushed 4y ago1 watchersCompare

[ Source](https://github.com/penobit/git-php)[ Packagist](https://packagist.org/packages/penobit/git-php)[ RSS](/packages/penobit-git-php/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

Git-PHP
=======

[](#git-php)

[![Downloads this Month](https://camo.githubusercontent.com/a71ac553a7f8b38d30acd526a60d4e7e0c173ef126e6fa5677938f1064beb7fb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f70656e6f6269742f6769742d7068702e737667)](https://packagist.org/packages/penobit/git-php)[![Latest Stable Version](https://camo.githubusercontent.com/87978bf409e5ff349c48bb68341e2ae842feed005ba8a039877b1a9ae307f53d/68747470733a2f2f706f7365722e707567782e6f72672f70656e6f6269742f6769742d7068702f762f737461626c65)](https://github.com/penobit/git-php/releases)[![License](https://camo.githubusercontent.com/fa7d5fcf2c84b580327af52da95dd751703af65f079dc3c5a0081beac0789718/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4e65772532304253442d626c75652e737667)](https://github.com/penobit/git-php/blob/master/license.md)[![Tests Status](https://github.com/penobit/git-php/workflows/Tests/badge.svg)](https://github.com/penobit/git-php/actions)

Library for work with Git repository in PHP.

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

[](#installation)

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

```
composer require penobit/git-php
```

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

Download Git installer:

> for [Linux](https://git-scm.com/download/linux)
>
> for [Windows](https://git-scm.com/download/win)
>
> for [others](https://git-scm.com/downloads)

Usage
-----

[](#usage)

```
$git = new Penobit\Git\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/penobit/git-php.git');

// Cloning of repository into own directory
$repo = $git->cloneRepository('https://github.com/penobit/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:penobit/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/penobit/git-php.git');
```

Troubleshooting - How to provide username and password for commands
-------------------------------------------------------------------

[](#troubleshooting---how-to-provide-username-and-password-for-commands)

1. use SSH instead of HTTPS -
2. store credentials to *Git Credential Storage*

- [Push github without entering username password windows git bash](http://www.tilcode.com/push-github-without-entering-username-password-windows-git-bash/)
- [Caching your github password in git](https://help.github.com/articles/caching-your-github-password-in-git/)
- [Git Tools Credential Storage](https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage)

1. [insert user and password into remote URL](https://stackoverflow.com/a/16381160)

- `git remote add origin https://user:password@server/path/repo.git`

2. [for `push()` you can use `--repo` argument](https://stackoverflow.com/a/12193555)

- `$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 \Penobit\Git\Git {
	public function open($directory) {
		return new OwnGitRepository($directory, $this->runner);
	}
}

class OwnGitRepository extends \Penobit\Git\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',
]);
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~41 days

Total

2

Last Release

1771d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8cbcaf2d9ebcdc5282f9dde8aeb9e6cff2d8bd8735b21669fb242e3a47dd7642?d=identicon)[penobit](/maintainers/penobit)

---

Top Contributors

[![penobit](https://avatars.githubusercontent.com/u/60283818?v=4)](https://github.com/penobit "penobit (5 commits)")

---

Tags

git

### Embed Badge

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

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

###  Alternatives

[brainmaestro/composer-git-hooks

Easily manage git hooks in your composer config

1.1k9.1M444](/packages/brainmaestro-composer-git-hooks)[captainhook/captainhook

PHP git hook manager

1.1k6.8M370](/packages/captainhook-captainhook)[czproject/git-php

Library for work with Git repository in PHP.

5544.9M81](/packages/czproject-git-php)[cypresslab/gitelephant

An abstraction layer for git written in PHP

6131.0M23](/packages/cypresslab-gitelephant)[teqneers/php-stream-wrapper-for-git

Git Stream Wrapper for PHP

2852.2M8](/packages/teqneers-php-stream-wrapper-for-git)[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

2511.3M109](/packages/marcocesarato-php-conventional-changelog)

PHPackages © 2026

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