PHPackages                             bazo/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. [CLI &amp; Console](/categories/cli)
4. /
5. bazo/git-wrapper

ActiveLibrary[CLI &amp; Console](/categories/cli)

bazo/git-wrapper
================

A command line wrapper aroung Git.

1.0.0beta7(13y ago)133GPL-3.0PHPPHP &gt;=5.3.0

Since Jan 10Pushed 12y ago1 watchersCompare

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

READMEChangelogDependencies (2)Versions (9)Used By (0)

Overview
========

[](#overview)

This library is a PHP wrapper around the Git command line tool. [![Build Status](https://camo.githubusercontent.com/3091e2eda01f010e57fcf27506de17074c25a2578c9847e8cfe4fcb329867f2b/68747470733a2f2f7472617669732d63692e6f72672f63706c69616b61732f6769742d777261707065722e706e673f6272616e63683d6d6173746572)](undefined)

Its purpose is to provide a readable API that abstracts some of the challenges of executing Git commands from within a PHP process. Specifically, this library builds upon the Symfony framework's Process component to execute the Git command in a way that works across platforms and uses the best-in-breed techniques available to PHP. This library also provides an SSH wrapper script and API method for developers to easily specify a private key other than one of the defaults by using the technique in [this thread on StackOverflow](http://stackoverflow.com/a/3500308/870667). Finally, various commands are expected to be executed in the directory containing the working copy. Although this a fairly simple challenge to overcome, the library handles this transparently so the developer doesn't have to think about it.

Usage
=====

[](#usage)

```
use GitWrapper\GitWrapper;

// Initialize the library. If the path to the Git binary is not passed as
// the first argument when instantiating GitWrapper, it is auto-discovered.
require_once 'vendor/autoload.php';
$wrapper = new GitWrapper();

// Optionally specify a private key other than one of the defaults.
$wrapper->setPrivateKey('/path/to/private/key');

// Get a working copy object, clone a repo into `./path/to/working/copy`.
$git = $wrapper->workingCopy('./path/to/working/copy');
$git->clone('git://github.com/cpliakas/git-wrapper.git');

// Create a file in the working copy.
touch('./path/to/working/copy/text.txt');

// Add it, commit it, and push the change.
$git
    ->add('test.txt')
    ->commit('Added the test.txt file as per the examples.')
    ->push();

// Render the output.
print $git->getOutput();

// Execute an arbitrary git command.
// The following is synonymous with `git config -l`
print $wrapper->git('config -l');
```

All command methods adhere to the following paradigm:

```
$git->command($arg1, $arg2, ..., $options);
```

Replace `command` with the Git command being executed, e.g. `checkout`, `push`, etc. The `$arg*` parameters are a variable number of arguments as they would be passed to the Git command line tool. `$options` is an optional array of command line options in the following format:

```
$options = array(
    'verbose' => true,   // Passes the "--verbose" flag.
    't' => 'my-branch',  // Passes the "-t my-branch" option.
);
```

Installation
============

[](#installation)

To install the required libraries, execute the following commands in the directory where this library is extracted.

```
curl -s https://getcomposer.org/installer | php
php composer.phar install

```

If curl is not available, replace the first command with the one below:

```
php -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));"

```

Please refer to the [Composer](http://getcomposer.org/) tool's [installation documentation](http://getcomposer.org/doc/00-intro.md#installation-nix)for more information.

Gotchas
=======

[](#gotchas)

There are a few "gotchas" that are out of scope for this library to solve but might prevent a successful implementation of running Git via PHP. The following is an incomplete list of challenges that are often encountered when executing Git from PHP.

Missing HOME Environment Variable
---------------------------------

[](#missing-home-environment-variable)

Sometimes the `HOME` environment variable is not set in the Git process that is spawned by PHP. This will cause many Git operations to fail. It is advisable to set the `HOME` environment variable to a path outside of the document root that the web server has write access to. Note that this environment variable is only set for the process running Git and NOT the PHP process that is spawns it.

```
$wrapper->setEnvVar('HOME', '/path/to/a/private/writable/dir');
```

It is important that the storage is persistent as the ~/.gitconfig file will be written to this location. See the following "gotcha" for why this is important.

Missing Identity And Configurations
-----------------------------------

[](#missing-identity-and-configurations)

Many repositories require that a name and email address are specified. This data is set by running `git config [name] [value]` on the command line, and the configurations are usually stored in the `~/.gitconfig file`. When executing Git via PHP, however, the process might have a different home directory than the user who normally runs git via the command line. Therefore no identity is sent to the repository, and it will likely throw an error.

```
// Set configuration options globally.
$wrapper->git('config --global user.name "User name"');
$wrapper->git('config --global user.email user@example.com');

// Set configuration options per repository.
$git
    ->config('user.name', 'User name')
    ->config('user.email', 'user@example.com');
```

Commits To Repositories With No Changes
---------------------------------------

[](#commits-to-repositories-with-no-changes)

Running `git commit` on a repository with no changes returns no output but exits with a status of 1. Therefore the library will throw a `GitException` since it correctly detected an error. It is advisable to check whether a working copy has any changes prior to running the commit operation in order to prevent unwanted exceptions.

```
if ($git->hasChanges()) {
    $git->commit('Committed the changes.');
}
```

Permissions Of The GIT\_SSH Wrapper Script
------------------------------------------

[](#permissions-of-the-git_ssh-wrapper-script)

On checkout, the bin/git-ssh-wrapper.sh script should be executable. If it is not, git commands with fail if a non-default private key is specified.

```
$> chmod 0755 ./bin/git-ssh-wrapper.sh

```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

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

Total

8

Last Release

4817d ago

### Community

Maintainers

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

---

Top Contributors

[![nickveenhof](https://avatars.githubusercontent.com/u/161341?v=4)](https://github.com/nickveenhof "nickveenhof (5 commits)")[![bazo](https://avatars.githubusercontent.com/u/181588?v=4)](https://github.com/bazo "bazo (3 commits)")[![cpliakas](https://avatars.githubusercontent.com/u/482722?v=4)](https://github.com/cpliakas "cpliakas (3 commits)")[![jrschumacher](https://avatars.githubusercontent.com/u/46549?v=4)](https://github.com/jrschumacher "jrschumacher (2 commits)")

---

Tags

git

### Embed Badge

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

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

###  Alternatives

[brainmaestro/composer-git-hooks

Easily manage git hooks in your composer config

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

A simple CLI tool for managing a project's git hooks across multiple members.

241281.3k20](/packages/projektgopher-whisky)[guanguans/ai-commit

Automagically generate conventional git commit message with AI. - 使用 AI 自动生成约定式 git 提交信息。

39231.2k10](/packages/guanguans-ai-commit)

PHPackages © 2026

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