cypresslab/gitelephant
GitElephant is a PHP OOP wrapper around the git CLI for managing repositories: inspect commits, branches, tags, diffs, logs, and statuses, and run common git operations via a clean API. Supports git >=1.8, PHP >=7.2 (older PHP via prior versions).
Installation
composer require cypresslab/gitelephant
Add the service provider to config/app.php:
'providers' => [
// ...
CypressLab\GitElephant\GitElephantServiceProvider::class,
],
Basic Usage Initialize GitElephant in a controller, command, or service:
use CypressLab\GitElephant\GitElephant;
$git = app(GitElephant::class);
$git->setRepository('/path/to/repo');
First Use Case: Clone a Repository
$git->clone('https://github.com/user/repo.git', '/local/path');
Repository Operations
$git->init('/path/to/repo');
$status = $git->status(); // Returns array of staged/unstaged files
$git->add(['file1.txt', 'file2.php']);
$git->commit('Update README');
Branching & Merging
$git->checkout('feature-branch');
$git->createBranch('feature-branch');
$git->merge('main');
Remote Operations
$git->addRemote('origin', 'https://github.com/user/repo.git');
$git->removeRemote('origin');
$git->fetch('origin');
$git->pull('origin', 'main');
$git->push('origin', 'feature-branch');
Tagging & Releases
$git->tag('v1.0.0', 'Release 1.0');
$git->deleteTag('v1.0.0');
Laravel Artisan Commands Use GitElephant in custom commands for deployments or CI/CD hooks:
use CypressLab\GitElephant\GitElephant;
class DeployCommand extends Command {
protected $git;
public function __construct(GitElephant $git) {
parent::__construct();
$this->git = $git;
}
public function handle() {
$this->git->pull('origin', 'main');
// Run deployment logic...
}
}
Event Listeners Trigger actions on Git events (e.g., post-commit hooks):
use CypressLab\GitElephant\Events\GitEvent;
public function handle(GitEvent $event) {
if ($event->action === 'commit') {
// Notify team or update database
}
}
Queue Jobs Offload long-running Git operations (e.g., large clones) to queues:
CloneRepo::dispatch('https://github.com/user/repo.git', '/storage/repos')->onQueue('git');
Repository Path Handling
$git->setRepository(__DIR__ . '/../../repo'); // Use absolute paths
Git Binary Dependencies
git binary. Verify it’s installed and in $PATH.which git # Should return /usr/bin/git or similar
Permission Issues
~/.ssh/id_rsa is configured and added to ssh-agent.git config --global credential.helper store to cache credentials.Detached HEAD States
checkout('commit-hash') may leave the repo in a detached HEAD state.$git->checkout('main'); // Return to a branch
Large Repositories
$git->clone('--depth=1', 'https://github.com/user/repo.git'); // Shallow clone
Enable Verbose Output
Pass --verbose to Git commands:
$git->setVerbose(true); // Logs raw Git output
Check Return Codes
GitElephant returns false on failure. Inspect $git->getLastError():
if (!$git->pull('origin', 'main')) {
$this->error($git->getLastError());
}
Log Git Commands
Override GitElephant to log executed commands:
$git->setLogger(function ($command) {
\Log::debug("Executed: {$command}");
});
Custom Git Commands
Extend GitElephant to support custom Git commands:
$git->execute('git submodule update --init');
Hooks & Events
Subscribe to Git events (e.g., pre-commit, post-merge):
$git->on('pre-commit', function () {
// Run linters, tests, etc.
});
Repository Factories Create reusable repo configurations:
$repo = new \CypressLab\GitElephant\Repository('/path/to/repo');
$repo->setRemote('origin', 'https://github.com/user/repo.git');
$git->useRepository($repo);
SSH Key Management For automated deployments, configure SSH keys programmatically:
$git->setSshKey('/path/to/private_key');
$git->setSshPassphrase('your_passphrase');
Git Config Overrides Override global Git settings per repository:
$git->config('user.name', 'GitElephant');
$git->config('user.email', 'git@elephant.com');
Case Sensitivity Branch/tag names are case-sensitive on some systems (e.g., Linux). Normalize case if needed:
$branch = strtolower($git->getCurrentBranch());
How can I help you explore Laravel packages today?