PHPackages                             leapt/git-wrapper - 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. leapt/git-wrapper

ActiveLibrary

leapt/git-wrapper
=================

An object oriented wrapper to run any Git command

v2.1.0(8mo ago)2643[2 issues](https://github.com/leapt/git-wrapper/issues)MITPHPPHP ^8.2CI passing

Since Feb 26Pushed 8mo agoCompare

[ Source](https://github.com/leapt/git-wrapper)[ Packagist](https://packagist.org/packages/leapt/git-wrapper)[ Docs](https://github.com/leapt/git-wrapper)[ RSS](/packages/leapt-git-wrapper/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (6)Dependencies (2)Versions (8)Used By (0)

Leapt Git Wrapper
=================

[](#leapt-git-wrapper)

[![Package version](https://camo.githubusercontent.com/15e89dd97cd74ecc63133c8d19d282e86ecd67e0224adeff8070a765b46bdfa0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c656170742f6769742d777261707065722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/leapt/git-wrapper)[![Build Status](https://camo.githubusercontent.com/99f6adc7db01cdf8f158c0ef43dfe8ab9f7074ea303b98fdf6719a7fefc8077c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6c656170742f6769742d777261707065722f636f6e74696e756f75732d696e746567726174696f6e2e796d6c3f6272616e63683d6d61696e267374796c653d666c61742d737175617265)](https://github.com/leapt/git-wrapper/actions?query=workflow%3A%22Continuous+Integration%22)[![PHP Version](https://camo.githubusercontent.com/463072f8fd28d1063fa1141e7314f291d44ab9b53c98d8ff5be3ea6a7a068877/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6c656170742f6769742d777261707065722e7376673f6272616e63683d6d61696e267374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/463072f8fd28d1063fa1141e7314f291d44ab9b53c98d8ff5be3ea6a7a068877/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6c656170742f6769742d777261707065722e7376673f6272616e63683d6d61696e267374796c653d666c61742d737175617265)[![License](https://camo.githubusercontent.com/10e85a5778fe7601504a17ecd18dfa7097f473186b0f947bc10db2d3e4f530e4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d7265642e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Code coverage](https://camo.githubusercontent.com/07a3002dcfd2d2502171fffc98fd9807bb025b4b1ec5a03813b3c30e0c0417d1/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6c656170742f6769742d777261707065723f7374796c653d666c61742d737175617265)](https://codecov.io/gh/leapt/git-wrapper/branch/main)

Allows to manage a Git repository with PHP. Provides an object-oriented wrapper to run any Git command.

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

[](#requirements)

- PHP &gt;= 8.2
- Git &gt;= 1.5

Instantiate a Repository
------------------------

[](#instantiate-a-repository)

```
use Leapt\GitWrapper\Repository;
$repository = new Repository('/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 repository
-----------------------

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

If the Git repository does not exist yet on file system, `Repository` can create it for you.

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

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

Run git commands
----------------

[](#run-git-commands)

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

```
// change current branch to main
$repository->git('checkout main');

// pull from a remote
$repository->git('pull origin main');

// add a remote repo
$repository->git('remote add origin https://github.com/leapt/git-wrapper.git');
```

There are no limitations, you can run any git commands.

The `git()` method returns the output string:

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

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

```

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

```
$repository->git('unknown'); // this git command does NOT exist: throw GitRuntimeException
```

Get branches information
------------------------

[](#get-branches-information)

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

### Get the branches list

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

```
$branches = $repository->getBranches();
// returns ['main', 'other_branch']
```

### Get the current branch

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

```
$branch = $repository->getCurrentBranch();
// returns 'main'
```

### Know if the repo has a given branch

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

```
$hasBranch = $repository->hasBranch('main');
// returns true
```

Get tags information
--------------------

[](#get-tags-information)

### Get the tags list:

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

```
$tags = $repository->getTags();
// returns ['first_release', 'v2']
```

Get commits information
-----------------------

[](#get-commits-information)

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

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

Internally, this method runs `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
                [committer] => 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 most recent one.

You can also retrieve the last commit by calling `$repository->getLastCommit()`.

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

[](#debug-mode)

`Repository` constructor's second parameter lets you enable debug mode. When debug mode is on, commands and their output are displayed.

```
$repository = new Repository('/path/to/the/git/repo', true);
```

Configure
---------

[](#configure)

`Repository` can be configured by passing an array of options to the constructor's third parameter.

### Change git executable path

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

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

```
$repo = new Repository('/path/to/the/git/repo', false, ['git_executable' => '/usr/bin/git']);
```

On most Unix system, it's `/usr/bin/git`. On Windows, it may be `C:\Program Files\Git\bin`.

### Change the command class

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

By default, the `Repository` class will use the `Command` class to implement Git commands. By replacing this option, you can use your own command implementation (which must implement the `Leapt\GitWrapper\CommandInterface`):

```
use Leapt\GitWrapper\Repository;
$repository = new Repository('/path/to/the/git/repo', false, ['command_class' => YourCommand::class]);
```

Contributing
------------

[](#contributing)

Feel free to contribute, like sending [pull requests](https://github.com/leapt/git-wrapper/pulls) to add features/tests or [creating issues](https://github.com/leapt/git-wrapper/issues) :)

Note there are a few helpers to maintain code quality, that you can run using these commands:

### PHPStan

[](#phpstan)

```
composer install --working-dir=tools/phpstan
tools/phpstan/vendor/bin/phpstan analyze
```

### PHP CS Fixer

[](#php-cs-fixer)

```
composer install --working-dir=tools/php-cs-fixer
# Check what can be fixed
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run --diff
# Fix them
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --diff
```

### PHPUnit

[](#phpunit)

```
vendor/bin/phpunit # Run tests
composer test # An alias to run tests
```

Note: tests rely on the default branch being `main`. This can be updated using the following command:

```
git config --global init.defaultBranch main
```

History
-------

[](#history)

This package is a maintained fork of the [ornicar/php-git-repo](https://github.com/ornicar/php-git-repo) package.

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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 ~235 days

Recently: every ~191 days

Total

8

Last Release

261d ago

Major Versions

v1.3.0 → v2.0.02023-12-05

PHP version history (2 changes)v1.0.0PHP ^8.0

v2.0.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/ae7ce9ceaf36b7aa89ec7bb76911d5d07e7c691bd9a4e40c9342c998f86248db?d=identicon)[leapt](/maintainers/leapt)

---

Top Contributors

[![jmsche](https://avatars.githubusercontent.com/u/3929498?v=4)](https://github.com/jmsche "jmsche (45 commits)")[![ornicar](https://avatars.githubusercontent.com/u/140370?v=4)](https://github.com/ornicar "ornicar (44 commits)")[![mbontemps](https://avatars.githubusercontent.com/u/231249?v=4)](https://github.com/mbontemps "mbontemps (4 commits)")[![mosch](https://avatars.githubusercontent.com/u/224866?v=4)](https://github.com/mosch "mosch (2 commits)")[![allmarkedup](https://avatars.githubusercontent.com/u/126726?v=4)](https://github.com/allmarkedup "allmarkedup (1 commits)")

---

Tags

git

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/leapt-git-wrapper/health.svg)

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

###  Alternatives

[captainhook/captainhook

PHP git hook manager

1.1k6.8M370](/packages/captainhook-captainhook)[cypresslab/gitelephant

An abstraction layer for git written in PHP

6131.0M23](/packages/cypresslab-gitelephant)[klaussilveira/gitter

Gitter allows you to interact in an object oriented manner with Git repositories.

20032.4k5](/packages/klaussilveira-gitter)[fortrabbit/craft-copy

Tooling for Craft on fortrabbit

7617.2k1](/packages/fortrabbit-craft-copy)[laravelplus/laravel-updater

A Laravel package for syncing with upstream repositories (GitHub, GitLab, Bitbucket, etc.)

201.7k](/packages/laravelplus-laravel-updater)

PHPackages © 2026

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