PHP code example of bit3 / git-php

1. Go to this page and download the library: Download bit3/git-php 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/ */

    

bit3 / git-php example snippets


$git->command()->option()->execute();

use Bit3\GitPhp\GitRepository;

$directory = '/path/to/git/target/directory';

$git = new GitRepository($directory);
$git->init()->execute();

use Bit3\GitPhp\GitRepository;

$directory = '/path/to/git/target/directory';

$git = new GitRepository($directory);
$git->cloneRepository()->execute();

$annotatedTag   = $git->describe()->execute();
$lightweightTag = $git->describe()->tags()->execute();
$recentRef      = $git->describe()->all()->execute();

$git->remote()
    ->setUrl('origin', '[email protected]:bit3/git-php.git')
    ->execute();

$git->remote()
    ->setPushUrl('origin', '[email protected]:bit3/git-php.git')
    ->execute();

$git->remote()
    ->add('github', '[email protected]:bit3/git-php.git')
    ->execute();

$git->fetch()->execute('github');

$git->checkout()->execute('hotfix/1.2.3');

$git->checkout()->execute('hotfix/1.2.3', '/fileA', '/fileB', '/dir/fileC');

$git->push()->execute('github', 'hotfix/1.2.3');

$git->add()->execute('file/to/add.ext');

$git->rm()->execute('file/to/remove.ext');

$git->commit()->message('Commit message')->execute();

$git->tag()->execute('v1.2.3');

$remotes = $git->remote()->getNames();

// array(
//     'origin',
//     'composer',
// )

$remotes = $git->branch()->getNames();

// array(
//     'master',
//     'hotfix/1.2.3',
// )

$remotes = $git->branch()->remotes()->->getNames();

// array(
//     'origin/master',
//     'origin/hotfix/1.2.3',
//     'origin/release/4.5.6',
// )

$remotes = $git->branch()->all()->->getNames();

// array(
//     'master',
//     'hotfix/1.2.3',
//     'remotes/origin/master',
//     'remotes/origin/hotfix/1.2.3',
//     'remotes/origin/release/4.5.6',
// )

$status = $git->status()->getStatus();

// array(
//     'existing-file.txt'      => array('index' => 'D',   'worktree' => false),
//     'removed-but-staged.txt' => array('index' => 'D',   'worktree' => 'A'),
//     'staged-file.txt'        => array('index' => false, 'worktree' => 'A'),
//     'unknown-file.txt'       => array('index' => '?',   'worktree' => '?'),
// )

$status = $git->status()->getIndexStatus();

// array(
//     'existing-file.txt'      => 'D',
//     'removed-but-staged.txt' => 'D',
//     'staged-file.txt'        => false,
//     'unknown-file.txt'       => '?',
// )

$status = $git->status()->getWorkTreeStatus();

// array(
//     'existing-file.txt'      => 'worktree' => false,
//     'removed-but-staged.txt' => 'worktree' => 'A',
//     'staged-file.txt'        => 'worktree' => 'A',
//     'unknown-file.txt'       => 'worktree' => '?',
// )