2017 © Pedro Peláez
 

library git-php

Library for work with Git repository in PHP.

image

czproject/git-php

Library for work with Git repository in PHP.

  • Sunday, June 24, 2018
  • by janpecha
  • Repository
  • 11 Watchers
  • 115 Stars
  • 57,071 Installations
  • PHP
  • 10 Dependents
  • 0 Suggesters
  • 32 Forks
  • 4 Open issues
  • 25 Versions
  • 26 % Grown

The README.md

Git-PHP

Build Status Downloads this Month Latest Stable Version License, (*1)

Library for work with Git repository in PHP., (*2)

Donate, (*3)

Installation

Download a latest package or use Composer:, (*4)

composer require czproject/git-php

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

Git installers:, (*6)

  • for Linux - https://git-scm.com/download/linux
  • for Windows - https://git-scm.com/download/win
  • for others - https://git-scm.com/downloads

Usage

``` php $git = new CzProject\GitPhp\Git; // create repo object $repo = $git->open('/path/to/repo');, (*7)

// create a new file in repo $filename = $repo->getRepositoryPath() . '/readme.txt'; file_put_contents($filename, "Lorem ipsum dolor sit amet ");, (*8)

// commit $repo->addFile($filename); $repo->commit('init commit');, (*9)



Initialization of empty repository ---------------------------------- ``` php $repo = $git->init('/path/to/repo-directory');

With parameters:, (*10)

``` php $repo = $git->init('/path/to/repo-directory', [ '--bare', // creates bare repo ]);, (*11)



Cloning of repository --------------------- ``` php // Cloning of repository into subdirectory 'git-php' in current working directory $repo = $git->cloneRepository('https://github.com/czproject/git-php.git'); // Cloning of repository into own directory $repo = $git->cloneRepository('https://github.com/czproject/git-php.git', '/path/to/my/subdir');

Basic operations

``` php $repo->hasChanges(); // returns boolean $repo->commit('commit message'); $repo->merge('branch-name'); $repo->checkout('master');, (*12)

$repo->getRepositoryPath();, (*13)

// adds files into commit $repo->addFile('file.txt'); $repo->addFile('file1.txt', 'file2.txt'); $repo->addFile(['file3.txt', 'file4.txt']);, (*14)

// renames files in repository $repo->renameFile('old.txt', 'new.txt'); $repo->renameFile([ 'old1.txt' => 'new1.txt', 'old2.txt' => 'new2.txt', ]);, (*15)

// removes files from repository $repo->removeFile('file.txt'); $repo->removeFile('file1.txt', 'file2.txt'); $repo->removeFile(['file3.txt', 'file4.txt']);, (*16)

// adds all changes in repository $repo->addAllChanges();, (*17)




Branches -------- ``` php // 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

``` php // gets list of all tags in repository $repo->getTags();, (*18)

// creates new tag $repo->createTag('v1.0.0'); $repo->createTag('v1.0.0', $options); $repo->createTag('v1.0.0', [ '-m' => 'message', ]);, (*19)

// renames tag $repo->renameTag('old-tag-name', 'new-tag-name');, (*20)

// removes tag $repo->removeTag('tag-name');, (*21)



History ------- ``` php // 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

``` php // pulls changes from remote $repo->pull('remote-name', ['--options']); $repo->pull('origin');, (*22)

// pushs changes to remote $repo->push('remote-name', ['--options']); $repo->push('origin'); $repo->push(['origin', 'master'], ['-u']);, (*23)

// fetchs changes from remote $repo->fetch('remote-name', ['--options']); $repo->fetch('origin'); $repo->fetch(['origin', 'master']);, (*24)

// adds remote repository $repo->addRemote('remote-name', 'repository-url', ['--options']); $repo->addRemote('origin', 'git@github.com:czproject/git-php.git');, (*25)

// renames remote $repo->renameRemote('old-remote-name', 'new-remote-name'); $repo->renameRemote('origin', 'upstream');, (*26)

// removes remote $repo->removeRemote('remote-name'); $repo->removeRemote('origin');, (*27)

// changes remote URL $repo->setRemoteUrl('remote-name', 'new-repository-url'); $repo->setRemoteUrl('upstream', 'https://github.com/czproject/git-php.git');, (*28)


**Troubleshooting - How to provide username and password for commands** 1) use SSH instead of HTTPS - https://stackoverflow.com/a/8588786 2) store credentials to *Git Credential Storage* * http://www.tilcode.com/push-github-without-entering-username-password-windows-git-bash/ * https://help.github.com/articles/caching-your-github-password-in-git/ * https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage 3) insert user and password into remote URL - https://stackoverflow.com/a/16381160 * `git remote add origin https://user:password@server/path/repo.git` 4) 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 -------------- For running other commands you can use `execute` method: ```php $output = $repo->execute('command'); $output = $repo->execute('command', 'with', 'parameters'); // example: $repo->execute('remote', 'set-branches', $originName, $branches);

Custom methods

You can create custom methods. For example:, (*29)

``` php class OwnGit extends \CzProject\GitPhp\Git { public function open($directory) { return new OwnGitRepository($directory, $this->runner); } }, (*30)

class OwnGitRepository extends \CzProject\GitPhp\GitRepository { public function setRemoteBranches($name, array $branches) { $this->run('remote', 'set-branches', $name, $branches); return $this; } }, (*31)

$git = new OwnGit; $repo = $git->open('/path/to/repo'); $repo->addRemote('origin', 'repository-url'); $repo->setRemoteBranches('origin', [ 'branch-1', 'branch-2', ]); ```, (*32)


License: New BSD License
Author: Jan Pecha, https://www.janpecha.cz/, (*33)

The Versions

24/06 2018

dev-master

9999999-dev

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

17/04 2018

v3.14.0

3.14.0.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

11/04 2018

v3.13.1

3.13.1.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

07/03 2018

v3.13.0

3.13.0.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

08/02 2018

v3.12.0

3.12.0.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

07/02 2018

v3.11.0

3.11.0.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

07/09 2017

3.10.0

3.10.0.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

02/06 2017

v3.9.1

3.9.1.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

13/03 2017

dev-wip-is-remote-readable

dev-wip-is-remote-readable

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

13/03 2017

v3.9.0

3.9.0.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

13/03 2017

v3.9.0-RC1

3.9.0.0-RC1

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

18/11 2016

v3.8.0

3.8.0.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

24/10 2016

v3.7.0

3.7.0.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

24/08 2016

v3.6.0

3.6.0.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

20/04 2016

v3.5.0

3.5.0.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

13/04 2016

v3.4.0

3.4.0.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

30/07 2015

v3.3.0

3.3.0.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

02/06 2015

v3.2.0

3.2.0.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

10/05 2015

v3.1.0

3.1.0.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

git

22/09 2013

v3.0.0

3.0.0.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

18/08 2013

dev-version-2

dev-version-2

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

18/08 2013

v2.0.0

2.0.0.0

Library for work with Git repository in PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

30/06 2013

v1.0.2

1.0.2.0

Library for work with Git from PHP.

  Sources   Download

BSD-3-Clause

19/04 2013

v1.0.1

1.0.1.0

Library for work with Git from PHP.

  Sources   Download

BSD-3-Clause

19/04 2013

v1.0.0

1.0.0.0

Library for work with Git from PHP.

  Sources   Download

BSD-3-Clause