PHP code example of ibrostudio / laravel-git

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

    

ibrostudio / laravel-git example snippets


use IBroStudio\Git\Repository;

// Open a repository from a local path
$repository = Repository::open('/path/to/repository');

use IBroStudio\Git\Repository;

// Clone a repository
$repository = Repository::clone(
    url: '[email protected]:username/repository.git',
    localParentDirectoryPath: '/path/to/parent/directory'
);

use IBroStudio\Git\Repository;

$repository = Repository::init([
    'name' => 'new-repository',
    'localParentDirectory' => '/path/to/parent/directory',
]);

use IBroStudio\Git\Repository;
use IBroStudio\DataObjects\Enums\GitProvidersEnum;
use IBroStudio\Git\Dto\OwnerDto\AuthOwnerDto;
use IBroStudio\Git\Dto\RepositoryDto\ConfigDto\RemoteDto;
use IBroStudio\DataObjects\ValueObjects\GitSshUrl;
use IBroStudio\Git\Enums\GitRepositoryVisibilitiesEnum;

$repository = Repository::init([
    'name' => 'new-repository',
    'branch' => 'main',
    'owner' => AuthOwnerDto::from(['name' => 'your-github-username']),
    'provider' => GitProvidersEnum::GITHUB,
    'remote' => RemoteDto::from([
        'name' => 'origin',
        'url' => GitSshUrl::build(GitProvidersEnum::GITHUB, 'your-github-username', 'new-repository'),
    ]),
    'localParentDirectory' => '/path/to/parent/directory',
    'visibility' => GitRepositoryVisibilitiesEnum::PRIVATE,
]);

// Check repository status
$status = $repository->status();
$hasChanges = $repository->hasChanges();

// Fetch, pull, and push
$repository->fetch();
$repository->pull();
$repository->push();

// Restore changes
$repository->restore();

// Get the last commit
$lastCommit = $repository->commits()->last();

// Get commit history
$history = $repository->commits()->history();

// Create a new commit
use IBroStudio\Git\Dto\RepositoryDto\CommitDto;

$commit = CommitDto::from([
    'message' => 'Your commit message',
    'description' => 'Optional longer description',
]);

$repository->commits()->add($commit);

// Undo the last commit
$repository->commits()->undo();

// Create a new branch
$repository->branches()->create('feature/new-feature');

// Switch to a branch
$repository->branches()->checkout('feature/new-feature');

// Merge branches
$repository->branches()->merge('feature/new-feature', 'main');

// Create a tag
$repository->tags()->create('v1.0.0');

// Create a release
use IBroStudio\Git\Dto\RepositoryDto\ReleaseDto;

$release = ReleaseDto::from([
    'tag' => 'v1.0.0',
    'name' => 'Version 1.0.0',
    'description' => 'Release notes for version 1.0.0',
]);

$repository->releases()->create($release);

$repository->changelog()->generate();
bash
php artisan vendor:publish --provider="IBroStudio\Git\GitServiceProvider"