PHP code example of rodziu / php-git

1. Go to this page and download the library: Download rodziu/php-git library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rodziu / php-git example snippets


$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
$head = $gitRepository->getHead();
$head->getCommitHash(); // commit hash that current head points to
$head->getBranch(); // current branch, null if head is detached

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
$gitRepository->getBranches(); // returns an array of local branch names
$gitRepository->getBranches(remote: true); // returns an array of remote branch names

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
foreach ($gitRepository->getTags() as $tag) {
    $tag; // \Rodziu\Git\Objects\Tag or \Rodziu\Git\Objects\AnnotatedTag
    $tag->getName();
    $tag->getTaggedObjectHash();
    ...
} 

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
foreach ($gitRepository->getLog() as $commit) {
    $commit; // \Rodziu\Git\Objects\Commit object 
    $commit->getMessage();
    $commit->getCommitDate();
    ...
}

// get origin/master branch log
$gitRepository->getLog('origin/master');

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
$gitRepository->getCommit('commit-hash'); // \Rodziu\Git\Objects\Commit object
...

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
$gitRepository->describe(); // describe current HEAD with annotated tags
$gitRepository->describe('commit-ish', all: true); // describe given ref as in git describe --all
$gitRepository->describe('commit-ish', tags: true); // describe given ref as in git describe --tags

\Rodziu\Git\GitRepository::cloneRepository(
    'https://your.repository.url/repository-name.git',
    '/destination/path/'
);

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
$gitRepository->fetch('origin');

$gitRepository = new \Rodziu\Git\GitRepository('/path/to/your/project/.git');
$gitRepository->checkout('commit-ish');