PHPackages                             itsis/php-git-hg-repo - 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. itsis/php-git-hg-repo

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

itsis/php-git-hg-repo
=====================

An object oriented wrapper to run any Git command

112721PHP

Since Jun 24Pushed 10y ago1 watchersCompare

[ Source](https://github.com/dlevacher/php-git-hg-repo)[ Packagist](https://packagist.org/packages/itsis/php-git-hg-repo)[ RSS](/packages/itsis-php-git-hg-repo/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (1)

PHP Git Repo
============

[](#php-git-repo)

Manage a Git repository with PHP. Provide an object oriented wrapper to run any Git command.

Requirements
------------

[](#requirements)

- PHP &gt;= 5.2 (PHP 5.3 works fine)
- Git &gt;= 1.5
- Hg &gt;= 3.3.2

Instantiate a PHPGit
--------------------

[](#instantiate-a-phpgit)

```
$repo = new PHPGit('/path/to/the/git/repo');

```

It does NOT create a Git repo, but a PHP object to manipulate an existing Git repo.

Create a Git or HG repository
-----------------------------

[](#create-a-git-or-hg-repository)

If the Git repository does not exist yet on filesystem, PHPGit can create it for you.

```
$repo = PHPGit::create('/path/to/the/git/repo');

```

It runs `git init` and returns a PHPGit object.

If the Hg repository does not exist yet on filesystem, PHPHg can create it for you.

```
$repo = PHPHg::create('/path/to/the/hg/repo');

```

It runs `hg init` and returns a PHPGit object.

Run git/hg commands
-------------------

[](#run-githg-commands)

git/hg commands can be run with the same syntax as in the CLI. Some examples:

```
// change current branch to master
$repo->git('checkout master');
$repo->hg('up master')

// pull from a remote
$repo->git('pull origin master');
$repo->hg('pull origin master')

// add a remote repo
$repo->git('remote add origin git://github.com/ornicar/php-git-repo.git');

```

There is no limitation, you can run any git/hg command.

The git()/hg() method returns the output string:

```
echo $repo->git('log --oneline');

e30b70b Move test repo to system tmp dir, introduce Command
01fabb1 Add test repo
12a95e6 Add base class with basic unit test
58e7769 Fix readme
c14c9ec Initial commit

echo $repo->hg('status');

? README.md
M test.php

```

The git()/hg() method throws a GitRuntimeException if the command is invalid:

```
$repo->git('wtf'); // this git command does NOT exist: throw GitRuntimeException
$repos->hg('wtf'); // this git command does NOT exist: throw HgRuntimeException

```

Get branches informations
-------------------------

[](#get-branches-informations)

Some shortcut methods are provided to deal with branches in a convenient way.

### Get the branches list:

[](#get-the-branches-list)

//For now, only git have commands for Branch

```
$branches = $repo->getBranches();
// returns array('master', 'other_branch')

```

### Get the current branch:

[](#get-the-current-branch)

```
$branch = $repo->getCurrentBranch();
// returns 'master'

```

### Know if the repo has a given branch:

[](#know-if-the-repo-has-a-given-branch)

```
$hasBranch = $repo->hasBranch('master');
// returns true

```

Get tags informations
---------------------

[](#get-tags-informations)

//For now, only git have commands for Tag

### Get the tags list:

[](#get-the-tags-list)

```
$tags = $repo->getTags();
// returns array('first_release', 'v2')

```

Get commits informations
------------------------

[](#get-commits-informations)

You can get an array of the last commits on the current branch.

```
$commits = $repo->getCommits(15);
// returns an array of the 15 last commits

```

Internally, this methods run `git log` with formatted output. The return value should look like:

```
Array
(
    [0] => Array
        (
            [id] => affb0e84a11b4180b0fa0e5d36bdac73584f0d71
            [tree] => 4b825dc642cb6eb9a060e54bf8d69288fbee4904
            [author] => Array
                (
                    [name] => ornicar
                    [email] => myemail@gmail.com
                )

            [authored_date] => 2010-09-22 19:17:35 +0200
            [commiter] => Array
                (
                    [name] => ornicar
                    [email] => myemail@gmail.com
                )

            [committed_date] => 2010-09-22 19:17:35 +0200
            [message] => My commit message
        )

    [1] => Array
        (
            ...

```

The first commit is the more recent one.

Internally, this methods run `hg log -l` with formatted output. The return value should look like: String(" changeset: 113:87c310edec6f tag: tip user: Blondeau Gabriel date: Wed Apr 22 08:17:38 2015 +0000 summary: README.md edited online with Bitbucket

```
changeset:   112:281ec79f27cc
user:        Blondeau Gabriel
date:        Wed Apr 22 08:16:50 2015 +0000
summary:     test.php edited online with Bitbucket

changeset:   111:0588a1ea0385
user:        Blondeau Gabriel
date:        Wed Apr 22 08:14:21 2015 +0000
summary:     test.php edited online with Bitbucket ...")

```

Debug mode
----------

[](#debug-mode)

`PHPGit` / `PHPHg` constructors second parameter lets you enable debug mode. When debug mode is on, commands and their output are displayed.

```
$repo = new PHPGit('/path/to/the/git/repo', true);
$repo = new PHPHg('/path/to/the/hg/repo', true);

```

Configure
---------

[](#configure)

`PHPGit` / `PHPHg` can be configured by passing an array of options to the constructor third parameter.

### Change git executable path

[](#change-git-executable-path)

You may need to provide the path to the git executable.

```
$repo = new PHPGit('/path/to/the/git/repo', false, array('git_executable' => '/usr/bin/git'));
$repo = new PHPHg('/path/to/the/hg/repo', false, array('hg_executable' => '/usr/bin/hg'));

```

On most Unix system, it's `/usr/bin/git`. On Windows, it may be `C:\Program Files\Git\bin`. On most Unix system, it's `/usr/bin/hg`. On Windows, it may be `C:\Program Files\Hg\bin`. //I use tortoise so : `C:\Program Files\TortoiseHg\hg`

### Change the command class

[](#change-the-command-class)

By default, `PHPGit` / `PHPHg` will use `Command` class to implement Git commands. By replacing this option, you can use your own command implementation:

```
$repo = new PHPGit('/path/to/the/git/repo', false, array('command_class' => 'myGitCommand'));
$repo = new PHPHg('/path/to/the/hg/repo', false, array('command_class' => 'myHgCommand'));

```

Run test suite
--------------

[](#run-test-suite)

All code is fully unit tested. To run tests on your server, from a CLI, run

```
php /path/to/php-git-repo/prove.php

```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/1e59a3dce2ab62c117c6103a63610b3d37b12a5fc062c2e360b8b37ebfea91b8?d=identicon)[dlevacher](/maintainers/dlevacher)

---

Top Contributors

[![GabrielBlondeau-Kirby](https://avatars.githubusercontent.com/u/1778459?v=4)](https://github.com/GabrielBlondeau-Kirby "GabrielBlondeau-Kirby (33 commits)")

### Embed Badge

![Health badge](/badges/itsis-php-git-hg-repo/health.svg)

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

###  Alternatives

[wythe/logistics

查询快递物流信息

3041.6k1](/packages/wythe-logistics)

PHPackages © 2026

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