{"id":13828146,"url":"https://github.com/czproject/git-php","last_synced_at":"2025-05-14T02:06:44.274Z","repository":{"id":8132720,"uuid":"9550346","full_name":"czproject/git-php","owner":"czproject","description":"Library for work with Git repositories in PHP.","archived":false,"fork":false,"pushed_at":"2025-03-23T19:20:33.000Z","size":132,"stargazers_count":526,"open_issues_count":19,"forks_count":107,"subscribers_count":21,"default_branch":"master","last_synced_at":"2025-04-10T10:09:50.628Z","etag":null,"topics":["git","git-php","git-wrapper","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/czproject.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":".github/funding.yml","license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["https://donate.stripe.com/7sIcO2a9maTSg2A9AA","https://www.janpecha.cz/donate/git-php/"]}},"created_at":"2013-04-19T17:08:14.000Z","updated_at":"2025-04-03T08:11:27.000Z","dependencies_parsed_at":"2024-06-18T10:54:00.322Z","dependency_job_id":"bcbca0ef-9cbd-4421-84f6-1eb7d13fef05","html_url":"https://github.com/czproject/git-php","commit_stats":{"total_commits":168,"total_committers":18,"mean_commits":9.333333333333334,"dds":"0.11904761904761907","last_synced_commit":"6905d8c095215fd33a6ca27b03ed1ab614db6780"},"previous_names":[],"tags_count":42,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czproject%2Fgit-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czproject%2Fgit-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czproject%2Fgit-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czproject%2Fgit-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/czproject","download_url":"https://codeload.github.com/czproject/git-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254052713,"owners_count":22006716,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["git","git-php","git-wrapper","php"],"created_at":"2024-08-04T09:02:34.152Z","updated_at":"2025-05-14T02:06:39.249Z","avatar_url":"https://github.com/czproject.png","language":"PHP","readme":"Git-PHP\n=======\n\n[![Build Status](https://github.com/czproject/git-php/workflows/Build/badge.svg)](https://github.com/czproject/git-php/actions)\n[![Downloads this Month](https://img.shields.io/packagist/dm/czproject/git-php.svg)](https://packagist.org/packages/czproject/git-php)\n[![Latest Stable Version](https://poser.pugx.org/czproject/git-php/v/stable)](https://github.com/czproject/git-php/releases)\n[![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://github.com/czproject/git-php/blob/master/license.md)\n\n\nLibrary for work with Git repository in PHP.\n\n\u003ca href=\"https://www.janpecha.cz/donate/git-php/\"\u003e\u003cimg src=\"https://buymecoffee.intm.org/img/donate-banner.v1.svg\" alt=\"Donate\" height=\"100\"\u003e\u003c/a\u003e\n\n\nInstallation\n------------\n\n[Download a latest package](https://github.com/czproject/git-php/releases) or use [Composer](http://getcomposer.org/):\n\n```\ncomposer require czproject/git-php\n```\n\nLibrary requires PHP 7.4 or later and `git` client (path to Git must be in system variable `PATH`).\n\nGit installers:\n\n* for Linux - https://git-scm.com/download/linux\n* for Windows - https://git-scm.com/download/win\n* for others - https://git-scm.com/downloads\n\n\nUsage\n-----\n\n``` php\n$git = new CzProject\\GitPhp\\Git;\n// create repo object\n$repo = $git-\u003eopen('/path/to/repo');\n\n// create a new file in repo\n$filename = $repo-\u003egetRepositoryPath() . '/readme.txt';\nfile_put_contents($filename, \"Lorem ipsum\n\tdolor\n\tsit amet\n\");\n\n// commit\n$repo-\u003eaddFile($filename);\n$repo-\u003ecommit('init commit');\n```\n\n\nInitialization of empty repository\n----------------------------------\n\n``` php\n$repo = $git-\u003einit('/path/to/repo-directory');\n```\n\nWith parameters:\n\n``` php\n$repo = $git-\u003einit('/path/to/repo-directory', [\n\t'--bare', // creates bare repo\n]);\n```\n\n\nCloning of repository\n---------------------\n\n``` php\n// Cloning of repository into subdirectory 'git-php' in current working directory\n$repo = $git-\u003ecloneRepository('https://github.com/czproject/git-php.git');\n\n// Cloning of repository into own directory\n$repo = $git-\u003ecloneRepository('https://github.com/czproject/git-php.git', '/path/to/my/subdir');\n```\n\n\nBasic operations\n----------------\n\n``` php\n$repo-\u003ehasChanges();    // returns boolean\n$repo-\u003ecommit('commit message');\n$repo-\u003emerge('branch-name');\n$repo-\u003echeckout('master');\n\n$repo-\u003egetRepositoryPath();\n\n// adds files into commit\n$repo-\u003eaddFile('file.txt');\n$repo-\u003eaddFile('file1.txt', 'file2.txt');\n$repo-\u003eaddFile(['file3.txt', 'file4.txt']);\n\n// renames files in repository\n$repo-\u003erenameFile('old.txt', 'new.txt');\n$repo-\u003erenameFile([\n    'old1.txt' =\u003e 'new1.txt',\n    'old2.txt' =\u003e 'new2.txt',\n]);\n\n// removes files from repository\n$repo-\u003eremoveFile('file.txt');\n$repo-\u003eremoveFile('file1.txt', 'file2.txt');\n$repo-\u003eremoveFile(['file3.txt', 'file4.txt']);\n\n// adds all changes in repository\n$repo-\u003eaddAllChanges();\n```\n\n\n\nBranches\n--------\n\n``` php\n// gets list of all repository branches (remotes \u0026 locals)\n$repo-\u003egetBranches();\n\n// gets list of all local branches\n$repo-\u003egetLocalBranches();\n\n// gets name of current branch\n$repo-\u003egetCurrentBranchName();\n\n// creates new branch\n$repo-\u003ecreateBranch('new-branch');\n\n// creates new branch and checkout\n$repo-\u003ecreateBranch('patch-1', TRUE);\n\n// removes branch\n$repo-\u003eremoveBranch('branch-name');\n```\n\n\nTags\n----\n\n``` php\n// gets list of all tags in repository\n$repo-\u003egetTags();\n\n// creates new tag\n$repo-\u003ecreateTag('v1.0.0');\n$repo-\u003ecreateTag('v1.0.0', $options);\n$repo-\u003ecreateTag('v1.0.0', [\n\t'-m' =\u003e 'message',\n]);\n\n// renames tag\n$repo-\u003erenameTag('old-tag-name', 'new-tag-name');\n\n// removes tag\n$repo-\u003eremoveTag('tag-name');\n```\n\n\nHistory\n-------\n\n``` php\n// returns last commit ID on current branch\n$commitId = $repo-\u003egetLastCommitId();\n$commitId-\u003egetId(); // or (string) $commitId\n\n// returns commit data\n$commit = $repo-\u003egetCommit('734713bc047d87bf7eac9674765ae793478c50d3');\n$commit-\u003egetId(); // instance of CommitId\n$commit-\u003egetSubject();\n$commit-\u003egetBody();\n$commit-\u003egetAuthorName();\n$commit-\u003egetAuthorEmail();\n$commit-\u003egetAuthorDate();\n$commit-\u003egetCommitterName();\n$commit-\u003egetCommitterEmail();\n$commit-\u003egetCommitterDate();\n$commit-\u003egetDate();\n\n// returns commit data of last commit on current branch\n$commit = $repo-\u003egetLastCommit();\n```\n\n\nRemotes\n-------\n\n``` php\n// pulls changes from remote\n$repo-\u003epull('remote-name', ['--options']);\n$repo-\u003epull('origin');\n\n// pushs changes to remote\n$repo-\u003epush('remote-name', ['--options']);\n$repo-\u003epush('origin');\n$repo-\u003epush(['origin', 'master'], ['-u']);\n\n// fetchs changes from remote\n$repo-\u003efetch('remote-name', ['--options']);\n$repo-\u003efetch('origin');\n$repo-\u003efetch(['origin', 'master']);\n\n// adds remote repository\n$repo-\u003eaddRemote('remote-name', 'repository-url', ['--options']);\n$repo-\u003eaddRemote('origin', 'git@github.com:czproject/git-php.git');\n\n// renames remote\n$repo-\u003erenameRemote('old-remote-name', 'new-remote-name');\n$repo-\u003erenameRemote('origin', 'upstream');\n\n// removes remote\n$repo-\u003eremoveRemote('remote-name');\n$repo-\u003eremoveRemote('origin');\n\n// changes remote URL\n$repo-\u003esetRemoteUrl('remote-name', 'new-repository-url');\n$repo-\u003esetRemoteUrl('upstream', 'https://github.com/czproject/git-php.git');\n```\n\n**Troubleshooting - How to provide username and password for commands**\n\n1) use SSH instead of HTTPS - https://stackoverflow.com/a/8588786\n2) store credentials to *Git Credential Storage*\n\t* http://www.tilcode.com/push-github-without-entering-username-password-windows-git-bash/\n\t* https://help.github.com/articles/caching-your-github-password-in-git/\n\t* https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage\n3) insert user and password into remote URL - https://stackoverflow.com/a/16381160\n\t* `git remote add origin https://user:password@server/path/repo.git`\n4) for `push()` you can use `--repo` argument - https://stackoverflow.com/a/12193555\n\t* `$git-\u003epush(NULL, ['--repo' =\u003e 'https://user:password@server/path/repo.git']);`\n\n\nOther commands\n--------------\n\nFor running other commands you can use `execute` method:\n\n```php\n$output = $repo-\u003eexecute('command');\n$output = $repo-\u003eexecute('command', 'with', 'parameters');\n\n// example:\n$repo-\u003eexecute('remote', 'set-branches', $originName, $branches);\n```\n\n\nCustom methods\n--------------\n\nYou can create custom methods. For example:\n\n``` php\nclass OwnGit extends \\CzProject\\GitPhp\\Git\n{\n\tpublic function open($directory)\n\t{\n\t\treturn new OwnGitRepository($directory, $this-\u003erunner);\n\t}\n}\n\nclass OwnGitRepository extends \\CzProject\\GitPhp\\GitRepository\n{\n\tpublic function setRemoteBranches($name, array $branches)\n\t{\n\t\t$this-\u003erun('remote', 'set-branches', $name, $branches);\n\t\treturn $this;\n\t}\n}\n\n\n$git = new OwnGit;\n$repo = $git-\u003eopen('/path/to/repo');\n$repo-\u003eaddRemote('origin', 'repository-url');\n$repo-\u003esetRemoteBranches('origin', [\n\t'branch-1',\n\t'branch-2',\n]);\n```\n\n------------------------------\n\nLicense: [New BSD License](license.md)\n\u003cbr\u003eAuthor: Jan Pecha, https://www.janpecha.cz/\n","funding_links":["https://donate.stripe.com/7sIcO2a9maTSg2A9AA","https://www.janpecha.cz/donate/git-php/"],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczproject%2Fgit-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fczproject%2Fgit-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczproject%2Fgit-php/lists"}