Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Git Laravel Package

directorytree/git

Lightweight PHP wrapper for running Git commands on a server. Supports pull, fetch, reset (hard/soft), and remote management (get/get all/add/set URL). Requires PHP 7.3+ and a working directory set to your repo via chdir().

View on GitHub
Deep Wiki
Context7

Requirements

  • PHP >= 7.3

Installation

composer require directorytree/git

Before getting started, you must ensure you change PHP's working directory to the root of your Git repository using chdir():

// The current working directory:
chdir(getcwd());

// A specific directory:
chdir('/usr/sbin/httpd');

This package also assumes that Git has been installed and is available in your systems PATH, so it can be called globally.

Usage

Create a new Git instance, and set the remote you want to work with:

use DirectoryTree\Git\Git;

$git = new Git($remote = 'origin');

Available Commands

Pull

Returns true or false.

$git = new Git();

$successful = $git->pull('master');

$successful = $git->pull('v1.0.1');

Fetch

Returns true or false.

$git = new Git();

$successful = $git->fetch();

Reset

Returns true or false.

Note: A hard reset will always be performed by default unless specified otherwise.

$git = new Git();

$successful = $git->reset($commitOrTag = 'v0.0.9');

$successful = $git->reset($commitOrTag = 'v0.0.9', $mode = 'soft');

Remotes

Get

Returns an array of remote URLs (empty array on failure):

$urls = $git->getRemote('origin');
Get All

Returns an array of remotes, with their URLs (empty array on failure):

$remotes = $git->getRemotes();
Add

Returns true or false.

$success = $git->addRemote('origin', 'https://github.com/DirectoryTree/Git');
Set URL

Returns true or false.

$successful = $git->setRemoteUrl('origin', 'https://github.com/DirectoryTree/Git');
Remove

Returns true or false.

$successful = $git->removeRemote('origin');

Tags

Get All

Returns an array of tags (empty array on failure):

$tags = $git->getTags();
Get Current

Returns the current repository's tag (false on failure).

$currentTag = $git->getCurrentTag();
Get Latest

Returns the current repository's latest (false on failure).

$latestTag = $git->getLatestTag();
Get Next

Returns the current repository's tag that is directly after the given (false on failure).

$nextTag = $git->getNextTag('v1.0.0');
Get Previous
$previousTag = $git->getPreviousTag('v1.0.1');

Commits

Get All

Returns an array of commits (empty array on failure):

$commits = $git->getCommits();

$commits = $git->getCommits(['from' => '9d26e0']);

$commits = $git->getCommits(['from' => '9d26e0', 'to' => '8bf0de6']);
Get Between

Shorthand for the above method.

$commits = $git->getCommitsBetween($from = '9d26e0', $to = '8bf0de6');

Testing

Git uses the PHP package TitasGailius/terminal for running all commands. This means you can utilize it's testing framework for executing all Git commands:

use DirectoryTree\Git\Git;

class GitTest extends TestCase
{
    public function test_git_pull()
    {
        Terminal::fake([
            'git pull {{ $remote }} {{ $commit }} --ff-only' => Terminal::response()->successful()
        ]);

        $this->assertTrue((new Git)->pull('v1.0.0'));
    }
}
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport