phppkg/phpgit

A Git wrapper library for PHP 8.1+

v2.0.4 2022-11-05 11:50 UTC

README

GitHub tag (latest SemVer) Php Version Unit Tests Deploy Pages

中文说明

PhpGit - 💪 Git wrapper and feature extension library written in PHP.

The project is forked from https://github.com/kzykhys/PHPGit

Features

  • Quick run git commands, eg: clone,add,commit,merge
  • Git repo info fetch: status branch remote
  • Generate changelog by git log

Installation

Requirements

  • PHP 8.1+
  • Git

Method 1: directly composer require

composer require phppkg/phpgit

Method 2: update composer.json

Update your composer.json and run composer update

{
    "require": {
        "phppkg/phpgit": "dev-master"
    }
}

Basic Usage

<?php

require __DIR__ . '/vendor/autoload.php';

$git = PhpGit\Git::new();
$git->clone('https://github.com/phppkg/PhpGit.git', '/path/to/repo');
$git->setRepository('/path/to/repo');
$git->remote->add('production', 'git://example.com/your/repo.git');

$git->add('README.md');
$git->commit('Adds README.md');

$git->checkout('release');
$git->merge('master');

$git->push();
$git->push('production', 'release');

$git->tag->create('v1.0.1', 'release');

foreach ($git->tree('release') as $object) {
    if ($object['type'] == 'blob') {
        echo $git->show($object['file']);
    }
}

Git Info

$repo = PhpGit\Repo::new('/path/to/repo');

$remotes = $repo->getRemotes();

$url  = $repo->getRemoteUrl('origin');
$info = $repo->getRemoteInfo('origin');

var_dump($info);

Output:

object(PhpGit\Info\RemoteInfo)#35 (8) {
  ["type"]=> string(4) "http"
  ["name"]=> string(6) "origin"
  ["url"]=> string(34) "https://github.com/phppkg/phpgit.git"
  ["scheme"]=> string(5) "https"
  ["host"]=> string(10) "github.com"
  ["path"]=> string(11) "phppkg/phpgit"
  ["group"]=> string(4) "ulue"
  ["repo"]=> string(6) "phpgit"
}

Changelog

Provide quick generate formatted changelog.

Formatter

  • SimpleFormatter
  • MarkdownFormatter
  • GithubReleaseFormatter

Filter

  • KeywordFilter
  • KeywordsFilter
  • MsgLenFilter
  • WordsLenFilter

Example

use Toolkit\Cli\Color;
use PhpGit\Changelog\Formatter\GithubReleaseFormatter;
use PhpGit\Changelog\Formatter\SimpleFormatter;
use PhpGit\Changelog\GitChangeLog;

// this is built in log format.
// you can custom format string, but must be set an log parser by $gcl->setLineParser(new YourLineParser);
$logFormat = GitChangeLog::LOG_FMT_HSC;

$oldVersion = 'v0.2.1';
$newVersion = 'HEAD';

// get output by git log cmd:
//  `git log v0.2.1...HEAD --reverse --pretty=format:"%H | %s | %cn" --no-merges`
$c = PhpGit\Git::new()->newCmd('log');
$c->add("$oldVersion...$newVersion");
$c->add('--reverse');
$c->addf('--pretty=format:"%s"', $logFormat);

// get repo url
$info = PhpGit\Repo::new()->getRemoteInfo('origin');
$repoUrl = $info->getHttpUrl();
Color::info('repo URL: ' . $repoUrl);

// create object by output.
$gcl = GitChangeLog::new($c->getOutput());
$gcl->setLogFormat($logFormat);
$gcl->setRepoUrl($repoUrl);

// you can set output style. default is markdown.
// can also, you can custom your item formatter
$gcl->setItemFormatter(new GithubReleaseFormatter());
//$gcl->setItemFormatter(new SimpleFormatter());

Color::info('parse logs and generate changelog');

// parse and generate.
$str = $gcl->generate();

echo $str;

Run example php bin/chlog.php will see:

> git log v0.2.1...HEAD --reverse --pretty=format:"%H | %s | %cn" --no-merges
> git remote -v
[INFO] repo URL:https://github.com/phppkg/phpgit
[INFO] parse logs and generate changelog

### Fixed

 - fix get latest tag error on windows https://github.com/phppkg/phpgit/commit/b9892b0ec363e405fcb76b08ea971fb651b4d2dc

### Update

 - up: rename package org to phppkg https://github.com/phppkg/phpgit/commit/990e55c6beddf654819c323c2a18d329074399f9
 - update some info https://github.com/phppkg/phpgit/commit/"1110de8b5ef0406c837bcd65f607b6f9483c9154

### Feature

 - feat: add util for quick generate change log https://github.com/phppkg/phpgit/commit/50962c12d3f16cdbbd8c1f21bc17ff920842365e

API

Git commands

  • git add
    • $git->add(string|array|\Traversable $file, array $options = [])
  • git archive
    • $git->archive(string $file, string $tree = null, string|array|\Traversable $path = null, array $options = [])
  • git branch
    • $git->branch(array $options = [])
    • $git->branch->create(string $branch, string $startPoint = null, array $options = [])
    • $git->branch->move(string $branch, string $newBranch, array $options = [])
    • $git->branch->delete(string $branch, array $options = [])
  • git cat-file
    • $git->cat->blob(string $object)
    • $git->cat->type(string $object)
    • $git->cat->size(string $object)
  • git checkout
    • $git->checkout(string $branch, array $options = [])
    • $git->checkout->create(string $branch, string $startPoint = null, array $options = [])
    • $git->checkout->orphan(string $branch, string $startPoint = null, array $options = [])
  • git clone
    • $git->clone(string $repository, string $path = null, array $options = [])
  • git commit
    • $git->commit(string $message, array $options = [])
  • git config
    • $git->config(array $options = [])
    • $git->config->set(string $name, string $value, array $options = [])
    • $git->config->add(string $name, string $value, array $options = [])
  • git describe
    • $git->describe(string $committish = null, array $options = [])
    • $git->describe->tags(string $committish = null, array $options = [])
  • git fetch
    • $git->fetch(string $repository, string $refspec = null, array $options = [])
    • $git->fetch->all(array $options = [])
  • git init
    • $git->init(string $path, array $options = [])
  • git log
    • $git->log(string $revRange = '', string $path = null, array $options = [])
  • git merge
    • $git->merge(string|array|\Traversable $commit, string $message = null, array $options = [])
    • $git->merge->abort()
  • git mv
    • $git->mv(string|array|\Iterator $source, string $destination, array $options = [])
  • git pull
    • $git->pull(string $repository = null, string $refspec = null, array $options = [])
  • git push
    • $git->push(string $repository = null, string $refspec = null, array $options = [])
  • git rebase
    • $git->rebase(string $upstream = null, string $branch = null, array $options = [])
    • $git->rebase->continues()
    • $git->rebase->abort()
    • $git->rebase->skip()
  • git remote
    • $git->remote()
    • $git->remote->add(string $name, string $url, array $options = [])
    • $git->remote->rename(string $name, string $newName)
    • $git->remote->rm(string $name)
    • $git->remote->show(string $name)
    • $git->remote->prune(string $name = null)
    • $git->remote->head(string $name, string $branch = null)
    • $git->remote->head->set(string $name, string $branch)
    • $git->remote->head->delete(string $name)
    • $git->remote->head->remote(string $name)
    • $git->remote->branches(string $name, array $branches)
    • $git->remote->branches->set(string $name, array $branches)
    • $git->remote->branches->add(string $name, array $branches)
    • $git->remote->url(string $name, string $newUrl, string $oldUrl = null, array $options = [])
    • $git->remote->url->set(string $name, string $newUrl, string $oldUrl = null, array $options = [])
    • $git->remote->url->add(string $name, string $newUrl, array $options = [])
    • $git->remote->url->delete(string $name, string $url, array $options = [])
  • git reset
    • $git->reset(string|array|\Traversable $paths, string $commit = null)
    • $git->reset->soft(string $commit = null)
    • $git->reset->mixed(string $commit = null)
    • $git->reset->hard(string $commit = null)
    • $git->reset->merge(string $commit = null)
    • $git->reset->keep(string $commit = null)
    • $git->reset->mode(string $mode, string $commit = null)
  • git rm
    • $git->rm(string|array|\Traversable $file, array $options = [])
    • $git->rm->cached(string|array|\Traversable $file, array $options = [])
  • git shortlog
    • $git->shortlog(string|array|\Traversable $commits = HEAD)
    • $git->shortlog->summary(string $commits = HEAD)
  • git show
    • $git->show(string $object, array $options = [])
  • git stash
    • $git->stash()
    • $git->stash->save(string $message = null, array $options = [])
    • $git->stash->lists(array $options = [])
    • $git->stash->show(string $stash = null)
    • $git->stash->drop(string $stash = null)
    • $git->stash->pop(string $stash = null, array $options = [])
    • $git->stash->apply(string $stash = null, array $options = [])
    • $git->stash->branch(string $name, string $stash = null)
    • $git->stash->clear()
    • $git->stash->create()
  • git status
    • $git->status(array $options = [])
  • git tag
    • $git->tag()
    • $git->tag->create(string $tag, string $commit = null, array $options = [])
    • $git->tag->delete(string|array|\Traversable $tag)
    • $git->tag->verify(string|array|\Traversable $tag)
  • git ls-tree
    • $git->tree(string $branch = master, string $path = '')

git add

$git->add(string|array|\Traversable $file, array $options = [])

Add file contents to the index

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->add('file.txt');
$git->add('file.txt', ['force' => false, 'ignore-errors' => false);
Options
  • force (boolean) Allow adding otherwise ignored files
  • ignore-errors (boolean) Do not abort the operation

git archive

$git->archive(string $file, string $tree = null, string|array|\Traversable $path = null, array $options = [])

Create an archive of files from a named tree

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->archive('repo.zip', 'master', null, ['format' => 'zip']);
Options
  • format (boolean) Format of the resulting archive: tar or zip
  • prefix (boolean) Prepend prefix/ to each filename in the archive

git branch

$git->branch(array $options = [])

Returns an array of both remote-tracking branches and local branches

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$branches = $git->branch();
Output Example
[
    'master' => ['current' => true, 'name' => 'master', 'hash' => 'bf231bb', 'title' => 'Initial Commit'],
    'origin/master' => ['current' => false, 'name' => 'origin/master', 'alias' => 'remotes/origin/master']
]
Options
  • all (boolean) List both remote-tracking branches and local branches
  • remotes (boolean) List the remote-tracking branches

$git->branch->create(string $branch, string $startPoint = null, array $options = [])

Creates a new branch head named $branch which points to the current HEAD, or $startPoint if given

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->branch->create('bugfix');              // from current HEAD
$git->branch->create('patch-1', 'a092bf7s'); // from commit
$git->branch->create('1.0.x-fix', 'v1.0.2'); // from tag
Options
  • force (boolean) Reset $branch to $startPoint if $branch exists already

$git->branch->move(string $branch, string $newBranch, array $options = [])

Move/rename a branch and the corresponding reflog

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->branch->move('bugfix', '2.0');
Options
  • force (boolean) Move/rename a branch even if the new branch name already exists

$git->branch->delete(string $branch, array $options = [])

Delete a branch

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->branch->delete('2.0');

The branch must be fully merged in its upstream branch, or in HEAD if no upstream was set with --track or --set-upstream.

Options
  • force (boolean) Delete a branch irrespective of its merged status

git cat-file

$git->cat->blob(string $object)

Returns the contents of blob object

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$contents = $git->cat->blob('e69de29bb2d1d6434b8b29ae775ad8');

$git->cat->type(string $object)

Returns the object type identified by $object

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$type = $git->cat->type('e69de29bb2d1d6434b8b29ae775ad8');

$git->cat->size(string $object)

Returns the object size identified by $object

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$type = $git->cat->size('e69de29bb2d1d6434b8b29ae775ad8');

git checkout

$git->checkout(string $branch, array $options = [])

Switches branches by updating the index, working tree, and HEAD to reflect the specified branch or commit

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->checkout('develop');
Options
  • force (boolean) Proceed even if the index or the working tree differs from HEAD
  • merge (boolean) Merges local modification

$git->checkout->create(string $branch, string $startPoint = null, array $options = [])

Create a new branch and checkout

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->checkout->create('patch-1');
$git->checkout->create('patch-2', 'develop');
Options
  • force (boolean) Proceed even if the index or the working tree differs from HEAD

$git->checkout->orphan(string $branch, string $startPoint = null, array $options = [])

Create a new orphan branch, named <new_branch>, started from <start_point> and switch to it

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->checkout->orphan('gh-pages');
Options
  • force (boolean) Proceed even if the index or the working tree differs from HEAD

git clone

$git->clone(string $repository, string $path = null, array $options = [])

Clone a repository into a new directory

$git = new PhpGit\Git();
$git->clone('https://github.com/kzykhys/PhpGit.git', '/path/to/repo');
Options
  • shared (boolean) Starts out without any object of its own
  • bare (boolean) Make a bare GIT repository

git commit

$git->commit(string $message, array $options = [])

Record changes to the repository

$git = new PhpGit\Git();
$git->clone('https://github.com/kzykhys/PhpGit.git', '/path/to/repo');
$git->setRepository('/path/to/repo');
$git->add('README.md');
$git->commit('Fixes README.md');
Options
  • all (boolean) Stage files that have been modified and deleted
  • reuse-message (string) Take an existing commit object, and reuse the log message and the authorship information (including the timestamp) when creating the commit
  • squash (string) Construct a commit message for use with rebase --autosquash
  • author (string) Override the commit author
  • date (string) Override the author date used in the commit
  • cleanup (string) Can be one of verbatim, whitespace, strip, and default
  • amend (boolean) Used to amend the tip of the current branch

git config

$git->config(array $options = [])

Returns all variables set in config file

Options
  • global (boolean) Read or write configuration options for the current user
  • system (boolean) Read or write configuration options for all users on the current machine

$git->config->set(string $name, string $value, array $options = [])

Set an option

Options
  • global (boolean) Read or write configuration options for the current user
  • system (boolean) Read or write configuration options for all users on the current machine

$git->config->add(string $name, string $value, array $options = [])

Adds a new line to the option without altering any existing values

Options
  • global (boolean) Read or write configuration options for the current user
  • system (boolean) Read or write configuration options for all users on the current machine

git describe

$git->describe(string $committish = null, array $options = [])

Returns the most recent tag that is reachable from a commit

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->tag->create('v1.0.0');
$git->commit('Fixes #14');
echo $git->describe('HEAD', ['tags' => true]);
Output Example
v1.0.0-1-g7049efc
Options
  • all (boolean) Enables matching any known branch, remote-tracking branch, or lightweight tag
  • tags (boolean) Enables matching a lightweight (non-annotated) tag
  • always (boolean) Show uniquely abbreviated commit object as fallback

$git->describe->tags(string $committish = null, array $options = [])

Equivalent to $git->describe($committish, ['tags' => true]);

git fetch

$git->fetch(string $repository, string $refspec = null, array $options = [])

Fetches named heads or tags from one or more other repositories, along with the objects necessary to complete them

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'git://your/repo.git');
$git->fetch('origin');
Options
  • append (boolean) Append ref names and object names of fetched refs to the existing contents of .git/FETCH_HEAD
  • keep (boolean) Keep downloaded pack
  • prune (boolean) After fetching, remove any remote-tracking branches which no longer exist on the remote

$git->fetch->all(array $options = [])

Fetch all remotes

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'git://your/repo.git');
$git->remote->add('release', 'git://your/another_repo.git');
$git->fetch->all();
Options
  • append (boolean) Append ref names and object names of fetched refs to the existing contents of .git/FETCH_HEAD
  • keep (boolean) Keep downloaded pack
  • prune (boolean) After fetching, remove any remote-tracking branches which no longer exist on the remote

git init

$git->init(string $path, array $options = [])

Create an empty git repository or reinitialize an existing one

$git = new PhpGit\Git();
$git->init('/path/to/repo1');
$git->init('/path/to/repo2', array('shared' => true, 'bare' => true));
Options
  • shared (boolean) Specify that the git repository is to be shared amongst several users
  • bare (boolean) Create a bare repository

git log

$git->log(string $revRange = '', string $path = null, array $options = [])

Returns the commit logs

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$logs = $git->log(array('limit' => 10));
Output Example
[
    0 => [
        'hash'  => '1a821f3f8483747fd045eb1f5a31c3cc3063b02b',
        'name'  => 'John Doe',
        'email' => 'john@example.com',
        'date'  => 'Fri Jan 17 16:32:49 2014 +0900',
        'title' => 'Initial Commit'
    ],
    1 => [
        //...
    ]
]
Options
  • limit (integer) Limits the number of commits to show
  • skip (integer) Skip number commits before starting to show the commit output

git merge

$git->merge(string|array|\Traversable $commit, string $message = null, array $options = [])

Incorporates changes from the named commits into the current branch

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->merge('1.0');
$git->merge('1.1', 'Merge message', ['strategy' => 'ours']);
Options
  • no-ff (boolean) Do not generate a merge commit if the merge resolved as a fast-forward, only update the branch pointer
  • rerere-autoupdate (boolean) Allow the rerere mechanism to update the index with the result of auto-conflict resolution if possible
  • squash (boolean) Allows you to create a single commit on top of the current branch whose effect is the same as merging another branch
  • strategy (string) Use the given merge strategy
  • strategy-option (string) Pass merge strategy specific option through to the merge strategy

$git->merge->abort()

Abort the merge process and try to reconstruct the pre-merge state

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
try {
    $git->merge('dev');
} catch (PhpGit\Exception\GitException $e) {
    $git->merge->abort();
}

git mv

$git->mv(string|array|\Iterator $source, string $destination, array $options = [])

Move or rename a file, a directory, or a symlink

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->mv('UPGRADE-1.0.md', 'UPGRADE-1.1.md');
Options
  • force (boolean) Force renaming or moving of a file even if the target exists

git pull

$git->pull(string $repository = null, string $refspec = null, array $options = [])

Fetch from and merge with another repository or a local branch

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->pull('origin', 'master');

git push

$git->push(string $repository = null, string $refspec = null, array $options = [])

Update remote refs along with associated objects

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->push('origin', 'master');

git rebase

$git->rebase(string $upstream = null, string $branch = null, array $options = [])

Forward-port local commits to the updated upstream head

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->fetch('origin');
$git->rebase('origin/master');
Options
  • onto (string) Starting point at which to create the new commits
  • no-verify (boolean) Bypasses the pre-rebase hook
  • force-rebase (boolean) Force the rebase even if the current branch is a descendant of the commit you are rebasing onto

$git->rebase->continues()

Restart the rebasing process after having resolved a merge conflict

$git->rebase->abort()

Abort the rebase operation and reset HEAD to the original branch

$git->rebase->skip()

Restart the rebasing process by skipping the current patch

git remote

$git->remote()

Returns an array of existing remotes

$git = new PhpGit\Git();
$git->clone('https://github.com/kzykhys/Text.git', '/path/to/repo');
$git->setRepository('/path/to/repo');
$remotes = $git->remote();
Output Example
[
    'origin' => [
        'fetch' => 'https://github.com/kzykhys/Text.git',
        'push'  => 'https://github.com/kzykhys/Text.git'
    ]
]

$git->remote->add(string $name, string $url, array $options = [])

Adds a remote named $name for the repository at $url

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->fetch('origin');
Options
  • tags (boolean) With this option, git fetch <name> imports every tag from the remote repository
  • no-tags (boolean) With this option, git fetch <name> does not import tags from the remote repository

$git->remote->rename(string $name, string $newName)

Rename the remote named $name to $newName

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->rename('origin', 'upstream');

$git->remote->rm(string $name)

Remove the remote named $name

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->rm('origin');

$git->remote->show(string $name)

Gives some information about the remote $name

$git = new PhpGit\Git();
$git->clone('https://github.com/kzykhys/Text.git', '/path/to/repo');
$git->setRepository('/path/to/repo');
echo $git->remote->show('origin');
Output Example
\* remote origin
  Fetch URL: https://github.com/kzykhys/Text.git
  Push  URL: https://github.com/kzykhys/Text.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)

$git->remote->prune(string $name = null)

Deletes all stale remote-tracking branches under $name

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->prune('origin');

$git->remote->head(string $name, string $branch = null)

Alias of set()

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->head('origin');

$git->remote->head->set(string $name, string $branch)

Sets the default branch for the named remote

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->head->set('origin');

$git->remote->head->delete(string $name)

Deletes the default branch for the named remote

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->head->delete('origin');

$git->remote->head->remote(string $name)

Determine the default branch by querying remote

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->head->remote('origin');

$git->remote->branches(string $name, array $branches)

Alias of set()

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->branches('origin', array('master', 'develop'));

$git->remote->branches->set(string $name, array $branches)

Changes the list of branches tracked by the named remote

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->branches->set('origin', array('master', 'develop'));

$git->remote->branches->add(string $name, array $branches)

Adds to the list of branches tracked by the named remote

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->branches->add('origin', array('master', 'develop'));

$git->remote->url(string $name, string $newUrl, string $oldUrl = null, array $options = [])

Alias of set()

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->url('origin', 'https://github.com/text/Text.git');
Options
  • push (boolean) Push URLs are manipulated instead of fetch URLs

$git->remote->url->set(string $name, string $newUrl, string $oldUrl = null, array $options = [])

Sets the URL remote to $newUrl

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->url->set('origin', 'https://github.com/text/Text.git');
Options
  • push (boolean) Push URLs are manipulated instead of fetch URLs

$git->remote->url->add(string $name, string $newUrl, array $options = [])

Adds new URL to remote

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->url->add('origin', 'https://github.com/text/Text.git');
Options
  • push (boolean) Push URLs are manipulated instead of fetch URLs

$git->remote->url->delete(string $name, string $url, array $options = [])

Deletes all URLs matching regex $url

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->remote->add('origin', 'https://github.com/kzykhys/Text.git');
$git->remote->url->delete('origin', 'https://github.com');
Options
  • push (boolean) Push URLs are manipulated instead of fetch URLs

git reset

$git->reset(string|array|\Traversable $paths, string $commit = null)

Resets the index entries for all $paths to their state at $commit

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->reset();

$git->reset->soft(string $commit = null)

Resets the current branch head to $commit

Does not touch the index file nor the working tree at all (but resets the head to $commit, just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->reset->soft();

$git->reset->mixed(string $commit = null)

Resets the current branch head to $commit

Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->reset->mixed();

$git->reset->hard(string $commit = null)

Resets the current branch head to $commit

Resets the index and working tree. Any changes to tracked files in the working tree since $commit are discarded

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->reset->hard();

$git->reset->merge(string $commit = null)

Resets the current branch head to $commit

Resets the index and updates the files in the working tree that are different between $commit and HEAD, but keeps those which are different between the index and working tree (i.e. which have changes which have not been added). If a file that is different between $commit and the index has unstaged changes, reset is aborted

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->reset->merge();

$git->reset->keep(string $commit = null)

Resets the current branch head to $commit

Resets index entries and updates files in the working tree that are different between $commit and HEAD. If a file that is different between $commit and HEAD has local changes, reset is aborted.

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->reset->keep();

$git->reset->mode(string $mode, string $commit = null)

Resets the current branch head to $commit

Possibly updates the index (resetting it to the tree of $commit) and the working tree depending on $mode

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->reset->mode('hard');

git rm

$git->rm(string|array|\Traversable $file, array $options = [])

Remove files from the working tree and from the index

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->rm('CHANGELOG-1.0-1.1.txt', ['force' => true]);
Options
  • force (boolean) Override the up-to-date check
  • cached (boolean) Unstage and remove paths only from the index
  • recursive (boolean) Allow recursive removal when a leading directory name is given

$git->rm->cached(string|array|\Traversable $file, array $options = [])

Equivalent to $git->rm($file, ['cached' => true]);

Options
  • force (boolean) Override the up-to-date check
  • recursive (boolean) Allow recursive removal when a leading directory name is given

git shortlog

$git->shortlog(string|array|\Traversable $commits = HEAD)

Summarize 'git log' output

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$shortlog = $git->shortlog();
Output Example
[
    'John Doe <john@example.com>' => [
        0 => ['commit' => '589de67', 'date' => new \DateTime('2014-02-10 12:56:15 +0300'), 'subject' => 'Update README'],
        1 => ['commit' => '589de67', 'date' => new \DateTime('2014-02-15 12:56:15 +0300'), 'subject' => 'Update README'],
    ],
    //...
]

$git->shortlog->summary(string $commits = HEAD)

Suppress commit description and provide a commit count summary only

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$shortlog = $git->shortlog->summary();
Output Example
[
    'John Doe <john@example.com>' => 153,
    //...
]

git show

$git->show(string $object, array $options = [])

Shows one or more objects (blobs, trees, tags and commits)

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
echo $git->show('3ddee587e209661c8265d5bfd0df999836f6dfa2');
Options
  • format (string) Pretty-print the contents of the commit logs in a given format, where can be one of oneline, short, medium, full, fuller, email, raw and format:
  • abbrev-commit (boolean) Instead of showing the full 40-byte hexadecimal commit object name, show only a partial prefix

git stash

$git->stash()

Save your local modifications to a new stash, and run git reset --hard to revert them

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->stash();

$git->stash->save(string $message = null, array $options = [])

Save your local modifications to a new stash, and run git reset --hard to revert them.

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->stash->save('My stash');

$git->stash->lists(array $options = [])

Returns the stashes that you currently have

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$stashes = $git->stash->lists();
Output Example
[
    0 => ['branch' => 'master', 'message' => '0e2f473 Fixes README.md'],
    1 => ['branch' => 'master', 'message' => 'ce1ddde Initial commit'],
]

$git->stash->show(string $stash = null)

Show the changes recorded in the stash as a diff between the stashed state and its original parent

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
echo $git->stash->show('stash@{0}');
Output Example
 REAMDE.md |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

$git->stash->drop(string $stash = null)

Remove a single stashed state from the stash list

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->stash->drop('stash@{0}');

$git->stash->pop(string $stash = null, array $options = [])

Remove a single stashed state from the stash list and apply it on top of the current working tree state

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->stash->pop('stash@{0}');

$git->stash->apply(string $stash = null, array $options = [])

Like pop, but do not remove the state from the stash list

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->stash->apply('stash@{0}');

$git->stash->branch(string $name, string $stash = null)

Creates and checks out a new branch named starting from the commit at which the was originally created, applies the changes recorded in to the new working tree and index

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->stash->branch('hotfix', 'stash@{0}');

$git->stash->clear()

Remove all the stashed states

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->stash->clear();

$git->stash->create()

Create a stash (which is a regular commit object) and return its object name, without storing it anywhere in the ref namespace

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$commit = $git->stash->create();
Output Example
877316ea6f95c43b7ccc2c2a362eeedfa78b597d

git status

$git->status(array $options = [])

Returns the working tree status

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$status = $git->status();
Constants
  • StatusCommand::UNMODIFIED [=' '] unmodified
  • StatusCommand::MODIFIED [='M'] modified
  • StatusCommand::ADDED [='A'] added
  • StatusCommand::DELETED [='D'] deleted
  • StatusCommand::RENAMED [='R'] renamed
  • StatusCommand::COPIED [='C'] copied
  • StatusCommand::UPDATED_BUT_UNMERGED [='U'] updated but unmerged
  • StatusCommand::UNTRACKED [='?'] untracked
  • StatusCommand::IGNORED [='!'] ignored
Output Example
[
    'branch' => 'master',
    'changes' => [
        ['file' => 'item1.txt', 'index' => 'A', 'work_tree' => 'M'],
        ['file' => 'item2.txt', 'index' => 'A', 'work_tree' => ' '],
        ['file' => 'item3.txt', 'index' => '?', 'work_tree' => '?'],
    ]
]
Options
  • ignored (boolean) Show ignored files as well

git tag

$git->tag()

Returns an array of tags

$git = new PhpGit\Git();
$git->clone('https://github.com/kzykhys/PhpGit.git', '/path/to/repo');
$git->setRepository('/path/to/repo');
$tags = $git->tag();
Output Example
['v1.0.0', 'v1.0.1', 'v1.0.2']

$git->tag->create(string $tag, string $commit = null, array $options = [])

Creates a tag object

$git = new PhpGit\Git();
$git->setRepository('/path/to/repo');
$git->tag->create('v1.0.0');
Options
  • annotate (boolean) Make an unsigned, annotated tag object
  • sign (boolean) Make a GPG-signed tag, using the default e-mail address’s key
  • force (boolean) Replace an existing tag with the given name (instead of failing)

$git->tag->delete(string|array|\Traversable $tag)

Delete existing tags with the given names

$git->tag->verify(string|array|\Traversable $tag)

Verify the gpg signature of the given tag names

git ls-tree

$git->tree(string $branch = master, string $path = '')

Returns the contents of a tree object

$git = new PhpGit\Git();
$git->clone('https://github.com/kzykhys/PhpGit.git', '/path/to/repo');
$git->setRepository('/path/to/repo');
$tree = $git->tree('master');
Output Example
[
    ['mode' => '100644', 'type' => 'blob', 'hash' => '1f100ce9855b66111d34b9807e47a73a9e7359f3', 'file' => '.gitignore', 'sort' => '2:.gitignore'],
    ['mode' => '100644', 'type' => 'blob', 'hash' => 'e0bfe494537037451b09c32636c8c2c9795c05c0', 'file' => '.travis.yml', 'sort' => '2:.travis.yml'],
    ['mode' => '040000', 'type' => 'tree', 'hash' => '8d5438e79f77cd72de80c49a413f4edde1f3e291', 'file' => 'bin', 'sort' => '1:.bin'],
]

License

The MIT License

Author

Developed by Kazuyuki Hayashi (@kzykhys), Maintained by @inhere