Giter VIP home page Giter VIP logo

git-php's Introduction

Git-PHP

Library for work with Git repository in PHP.

Usage

<?php
	// create repo object
	$repo = new Cz\Git\GitRepository('/path/to/repo');

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

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

Initialization of empty repository

<?php
$repo = GitRepository::init('/path/to/repo-directory');

With parameters:

<?php
$repo = GitRepository::init('/path/to/repo-directory', array(
	'--bare', // creates bare repo
));

Cloning of repository

<?php
// Cloning of repository into subdirectory 'git-php' in current working directory
$repo = GitRepository::cloneRepository('https://github.com/czproject/git-php.git');

// Cloning of repository into own directory
$repo = GitRepository::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');

$repo->getRepositoryPath();

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

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

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

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

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();

// creates new tag
$repo->createTag('v1.0.0');

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

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

Remotes

<?php
// pulls changes from remote
$repo->pull('remote-name', array('--options'));
$repo->pull('origin');

// pushs changes to remote
$repo->push('remote-name', array('--options'));
$repo->push('origin');

// fetchs changes from remote
$repo->fetch('remote-name', array('--options'));
$repo->fetch('origin');

// adds remote repository
$repo->addRemote('remote-name', 'repository-url', array('--options'));
$repo->addRemote('origin', '[email protected]:czproject/git-php.git');

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

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

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

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
  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, array('--repo' => 'https://user:password@server/path/repo.git'));

Custom methods

You can create custom methods. For example:

class OwnGitRepository extends \Cz\Git\GitRepository
{
	public function setRemoteBranches($name, array $branches)
	{
		return $this->begin()
			->run('git remote set-branches', $name, $branches)
			->end();
	}
}


$repo = new OwnGitRepository('/path/to/repo');
$repo->addRemote('origin', 'repository-url');
$repo->setRemoteBranches('origin', array(
	'branch-1',
	'branch-2',
));

Installation

Download a latest package or use Composer:

composer require czproject/git-php

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

Git installers:


License: New BSD License
Author: Jan Pecha, https://www.janpecha.cz/

git-php's People

Contributors

gerryd avatar janpecha avatar mawalu avatar spongy83 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.