bit3/git-php
PHP library for working with Git repositories from code. Execute common Git commands, inspect repository state, and script Git operations with a simple API—useful for automation, deployment tools, and integrations that need Git access without shelling out manually.
Installation
composer require bit3/git-php
Add to composer.json if not using autoloading:
"autoload": {
"psr-4": {
"App\\": "app/",
"Bit3\\Git\\": "vendor/bit3/git-php/src/"
}
}
Run composer dump-autoload.
First Use Case: Cloning a Repository
use Bit3\Git\Git;
$git = new Git('/path/to/local/repo');
$git->clone('https://github.com/user/repo.git');
Key Entry Points
Git::__construct(string $path): Initialize with a local repo path.Git::clone(string $url): Clone a remote repo.Git::pull(): Fetch and merge remote changes.Git::commit(string $message): Commit staged changes.Where to Look First
Bit3\Git\Git namespace for core methods.// Initialize and clone
$git = new Git('/tmp/repo');
$git->clone('https://github.com/laravel/laravel.git');
// Check status
$status = $git->status(); // Returns array of changed files
// Create and switch branches
$git->branchCreate('feature/login');
$git->branchCheckout('feature/login');
// Merge branches
$git->branchMerge('main');
$git->add('path/to/file');
$git->commit('Update login logic');
$git->push('origin', 'feature/login');
$git->tagCreate('v1.0.0', 'Release 1.0.0');
$git->push('origin', null, null, ['--tags']);
Create a custom Artisan command:
namespace App\Console\Commands;
use Bit3\Git\Git;
use Illuminate\Console\Command;
class DeployCommand extends Command
{
protected $signature = 'deploy:pull';
public function handle()
{
$git = new Git(base_path());
$git->pull();
$this->info('Repository updated!');
}
}
try {
$git->pull();
} catch (\Bit3\Git\Exception $e) {
Log::error('Git error: ' . $e->getMessage());
}
$git->add('file.txt');
Log::debug('Added file.txt to staging');
.env:
GIT_REPO_PATH=/var/www/repo
$git = new Git(env('GIT_REPO_PATH'));
Path Handling
/var/www/repo vs. ./repo).realpath() to resolve relative paths:
$git = new Git(realpath(__DIR__ . '/../repo'));
Permission Issues
chmod -R 755 /path/to/repo
~/.ssh has correct permissions (chmod 700 ~/.ssh).Detached HEAD State
checkout or reset may leave the repo in a detached HEAD state. Reattach with:
$git->branchCheckout('main');
Case Sensitivity
$branch = strtolower('Feature/Login');
$git->branchCreate($branch);
Large Files
--depth=1 for shallow clones:
$git->clone('https://github.com/repo.git', ['--depth' => 1]);
-v flag via options:
$git->pull(['-v']);
$gitVersion = $git->version();
Log::debug('Git version: ' . $gitVersion);
Git::getLastCommand() to debug failed operations:
try {
$git->pull();
} catch (\Exception $e) {
Log::error('Failed command: ' . $git->getLastCommand());
}
Custom Git Hooks Extend the package by adding pre/post hooks:
$git->addEventListener('pre-commit', function() {
// Run tests before commit
exec('php artisan test');
});
(Note: Requires custom implementation; the package does not natively support hooks.)
Submodule Support For repos with submodules, manually initialize them:
$git->submoduleUpdate(['--init', '--recursive']);
Custom Git Config Set global/local config via:
$git->config('user.name', 'Your Name');
$git->config('user.email', 'you@example.com');
Parallel Operations Use Laravel’s queues to run long Git operations asynchronously:
GitOperation::dispatch($repoPath, 'pull')->onQueue('git');
(Requires custom job class.)
$git->clone('git@github.com:user/repo.git');
For HTTPS, use a personal access token in the URL:
$git->clone('https://user:token@github.com/user/repo.git');
$git->config('core.autocrlf', 'input'); // Preserve line endings
How can I help you explore Laravel packages today?