### Installing Git-PHP with Composer Source: https://github.com/czproject/git-php/blob/master/readme.md This snippet shows how to install the Git-PHP library using Composer, the PHP dependency manager. It adds `czproject/git-php` to your project's dependencies, making the library available for use. Requires Composer to be installed and configured. ```Shell composer require czproject/git-php ``` -------------------------------- ### Initializing a Bare Git Repository Source: https://github.com/czproject/git-php/blob/master/readme.md This example demonstrates initializing a new Git repository with additional parameters, specifically creating a bare repository. Bare repositories are typically used for central repositories on a server, as they do not have a working directory. ```PHP $repo = $git->init('/path/to/repo-directory', [ '--bare', // creates bare repo ]); ``` -------------------------------- ### Initializing an Empty Git Repository Source: https://github.com/czproject/git-php/blob/master/readme.md This snippet shows how to initialize a new, empty Git repository at a specified path. This is useful for starting a new project under version control. The `init` method creates the necessary Git directory structure. ```PHP $repo = $git->init('/path/to/repo-directory'); ``` -------------------------------- ### Cloning a Git Repository into a Custom Directory Source: https://github.com/czproject/git-php/blob/master/readme.md This example shows how to clone a remote Git repository into a specific, user-defined directory. This provides more control over where the cloned repository is placed on the local filesystem. ```PHP $repo = $git->cloneRepository('https://github.com/czproject/git-php.git', '/path/to/my/subdir'); ``` -------------------------------- ### Basic Git Operations with Git-PHP Source: https://github.com/czproject/git-php/blob/master/readme.md This example demonstrates how to open an existing Git repository, create a new file within it, add the file to the staging area, and commit the changes. It illustrates the fundamental workflow of modifying and committing files using the Git-PHP library. ```PHP $git = new CzProject\GitPhp\Git; // create repo object $repo = $git->open('/path/to/repo'); // create a new file in repo $filename = $repo->getRepositoryPath() . '/readme.txt'; file_put_contents($filename, "Lorem ipsum\n\tdolor\n\tsit amet\n"); // commit $repo->addFile($filename); $repo->commit('init commit'); ``` -------------------------------- ### Cloning a Git Repository into Default Directory Source: https://github.com/czproject/git-php/blob/master/readme.md This snippet illustrates how to clone a remote Git repository into a subdirectory named after the repository in the current working directory. It's a quick way to get a copy of a remote repository. ```PHP $repo = $git->cloneRepository('https://github.com/czproject/git-php.git'); ``` -------------------------------- ### Extending Git-PHP with Custom Methods (PHP) Source: https://github.com/czproject/git-php/blob/master/readme.md This comprehensive example demonstrates how to extend the Git-PHP library by creating custom `OwnGit` and `OwnGitRepository` classes. It shows how to add a new method, `setRemoteBranches`, to the repository object, allowing for custom Git operations to be encapsulated within the library's object-oriented structure. ```PHP class OwnGit extends \CzProject\GitPhp\Git { public function open($directory) { return new OwnGitRepository($directory, $this->runner); } } class OwnGitRepository extends \CzProject\GitPhp\GitRepository { public function setRemoteBranches($name, array $branches) { $this->run('remote', 'set-branches', $name, $branches); return $this; } } $git = new OwnGit; $repo = $git->open('/path/to/repo'); $repo->addRemote('origin', 'repository-url'); $repo->setRemoteBranches('origin', [ 'branch-1', 'branch-2', ]); ``` -------------------------------- ### Retrieving Git Commit History Information Source: https://github.com/czproject/git-php/blob/master/readme.md This snippet demonstrates how to access Git commit history details using the Git-PHP library. It shows how to get the ID of the last commit, retrieve full commit data for a specific commit ID, and get data for the very last commit on the current branch. This is useful for auditing and displaying repository history. ```PHP // returns last commit ID on current branch $commitId = $repo->getLastCommitId(); $commitId->getId(); // or (string) $commitId // returns commit data $commit = $repo->getCommit('734713bc047d87bf7eac9674765ae793478c50d3'); $commit->getId(); // instance of CommitId $commit->getSubject(); $commit->getBody(); $commit->getAuthorName(); $commit->getAuthorEmail(); $commit->getAuthorDate(); $commit->getCommitterName(); $commit->getCommitterEmail(); $commit->getCommitterDate(); $commit->getDate(); // returns commit data of last commit on current branch $commit = $repo->getLastCommit(); ``` -------------------------------- ### Performing Common Git Repository Operations Source: https://github.com/czproject/git-php/blob/master/readme.md This comprehensive snippet showcases various common Git operations available through the Git-PHP library, including checking for changes, committing, merging, checking out branches, getting the repository path, adding, renaming, and removing files, and staging all changes. It covers essential day-to-day Git commands. ```PHP $repo->hasChanges(); // returns boolean $repo->commit('commit message'); $repo->merge('branch-name'); $repo->checkout('master'); $repo->getRepositoryPath(); // adds files into commit $repo->addFile('file.txt'); $repo->addFile('file1.txt', 'file2.txt'); $repo->addFile(['file3.txt', 'file4.txt']); // renames files in repository $repo->renameFile('old.txt', 'new.txt'); $repo->renameFile([ 'old1.txt' => 'new1.txt', 'old2.txt' => 'new2.txt', ]); // removes files from repository $repo->removeFile('file.txt'); $repo->removeFile('file1.txt', 'file2.txt'); $repo->removeFile(['file3.txt', 'file4.txt']); // adds all changes in repository $repo->addAllChanges(); ``` -------------------------------- ### Managing Git Branches with Git-PHP Source: https://github.com/czproject/git-php/blob/master/readme.md This snippet demonstrates how to interact with Git branches using the Git-PHP library. It covers retrieving lists of all branches (local and remote), getting the current branch name, creating new branches (with optional checkout), and removing existing branches. These functions are crucial for managing development workflows. ```PHP // gets list of all repository branches (remotes & locals) $repo->getBranches(); // gets list of all local branches $repo->getLocalBranches(); // gets name of current branch $repo->getCurrentBranchName(); // creates new branch $repo->createBranch('new-branch'); // creates new branch and checkout $repo->createBranch('patch-1', TRUE); // removes branch $repo->removeBranch('branch-name'); ``` -------------------------------- ### Executing Arbitrary Git Commands (PHP) Source: https://github.com/czproject/git-php/blob/master/readme.md This snippet illustrates how to execute any Git command using the `execute` method of the `GitRepository` object. It allows passing the command and its parameters as separate arguments, providing flexibility for operations not directly exposed by the library's methods. ```PHP $output = $repo->execute('command'); $output = $repo->execute('command', 'with', 'parameters'); // example: $repo->execute('remote', 'set-branches', $originName, $branches); ``` -------------------------------- ### Pushing with Repository URL Argument (PHP) Source: https://github.com/czproject/git-php/blob/master/readme.md This snippet shows how to use the `--repo` argument with the `push()` method in the Git-PHP library to specify the repository URL, including credentials. This can be an alternative for providing authentication details during a push operation. ```PHP $git->push(NULL, ['--repo' => 'https://user:password@server/path/repo.git']); ``` -------------------------------- ### Managing Git Remotes and Remote Operations Source: https://github.com/czproject/git-php/blob/master/readme.md This comprehensive snippet illustrates how to manage remote Git repositories and perform common remote operations like pulling, pushing, and fetching changes. It also covers adding, renaming, and removing remote configurations. These functions are essential for collaborating and synchronizing with remote repositories. ```PHP // pulls changes from remote $repo->pull('remote-name', ['--options']); $repo->pull('origin'); // pushs changes to remote $repo->push('remote-name', ['--options']); $repo->push('origin'); $repo->push(['origin', 'master'], ['-u']); // fetchs changes from remote $repo->fetch('remote-name', ['--options']); $repo->fetch('origin'); $repo->fetch(['origin', 'master']); // adds remote repository $repo->addRemote('remote-name', 'repository-url', ['--options']); $repo->addRemote('origin', 'git@github.com:czproject/git-php.git'); // renames remote $repo->renameRemote('old-remote-name', 'new-remote-name'); $repo->renameRemote('origin', 'upstream'); // removes remote $repo->removeRemote('remote-name'); $repo->removeRemote('origin'); ``` -------------------------------- ### Managing Git Tags with Git-PHP Source: https://github.com/czproject/git-php/blob/master/readme.md This snippet illustrates how to manage Git tags using the Git-PHP library. It includes methods for listing all tags, creating new tags (with optional messages), renaming existing tags, and removing tags. Tags are used to mark specific points in history, typically for releases. ```PHP // gets list of all tags in repository $repo->getTags(); // creates new tag $repo->createTag('v1.0.0'); $repo->createTag('v1.0.0', $options); $repo->createTag('v1.0.0', [ '-m' => 'message', ]); // renames tag $repo->renameTag('old-tag-name', 'new-tag-name'); // removes tag $repo->removeTag('tag-name'); ``` -------------------------------- ### Adding Remote with Credentials (Git CLI) Source: https://github.com/czproject/git-php/blob/master/readme.md This Git command demonstrates how to add a new remote repository while embedding the username and password directly into the URL. This method is one way to provide credentials for Git operations, though other more secure methods like SSH or credential storage are generally preferred. ```Shell git remote add origin https://user:password@server/path/repo.git ``` -------------------------------- ### Setting Remote URL for Git Repository (PHP) Source: https://github.com/czproject/git-php/blob/master/readme.md This snippet demonstrates how to change or set the URL for a remote repository using the `setRemoteUrl` method. It takes the remote name and the new URL as parameters. This is useful for updating repository origins or mirrors. ```PHP $repo->setRemoteUrl('remote-name', 'new-repository-url'); $repo->setRemoteUrl('upstream', 'https://github.com/czproject/git-php.git'); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.