PHP code example of ggioffreda / git

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

    

ggioffreda / git example snippets




namespace MyNamespace;

use Gioffreda\Component\Git\Git;

class MyJob
{

    public function doSomething($remoteUri, $localPath)
    {
        // cloning a remote repository
        $git = Git::cloneRemote($remoteUri, $localPath);
        // switches to the develop branch
        $git->checkout('develop');

        // your logic here, change some files
        // ...

        $git
            // adds all the files
            ->add('.')
            // commits the changes to the develop branch
            ->commit('Changed some files')
            // switches to the master branch
            ->checkout('master')
            // merges the develop branch into the master branch
            ->merge('develop')
            // commits the changes into the master branch
            ->commit('Merged the changes into master.')
            // pushes the changes to the remote repository using a custom command line
            ->run(['push', '--all'])
        ;

        // or you can use a local one even if not initialized yet
        // new Git project using a custom executable
        $git = Git::create($localPath, '/usr/local/custom/git');
        $git
            // this will initialize the Git project if not initialized already
            ->init()
            // adds all the files in the folder ./src
            ->add('./src')
            // commits the changes
            ->commit('Initial commit (only sources).')
        ;

        // retrieves the last commits hashes and messages
        $logs = $git->getLogs();
        // retrieves the list of branches with latest commit hash and message
        $branches = $git->getBranches()

        // using git-flow, initializing it first
        $git->flow()->init();
        // starts a new feature
        $git->flow()->featureStart('test1');

        // your logic here, change some files
        // ...

        $git
            // mark the changes so they'll be committed
            ->add('.')
            // commits the the changes
            ->commit('feature finished')
            // finishes the feature
            ->flow()->featureFinish('test1')
        ;
    }

}