PhpRiot
Follow phpriot on Twitter
Sponsored Link
News Archive
PhpRiot Newsletter
Your Email Address:

More information

On feature-branches and pull-requests

Note: This article was originally published at Planet PHP on 25 May 8120.
Planet PHP

Everyone and their mother uses Git + GitHub combo these days. A lot of open-source projects accept patches using github's pull requests, becausea€¦ well, because it is the easiest way to review and accept patches.

But, novice Git users don't know how to do this optimally, and a€onaivea€¯ approach leads to complexities. Git is a distributed version control system, which means that everyone can a€ocommita€¯ to their copies of repositories. Syncing these commits with upstream is a bit more difficult and leads to commit-conflicts sometimes. So, here's aforementioned a€onaivea€¯ approach:

  1. Fork upstream github repository
  2. Clone forked repository to local machine
  3. Make changes
  4. Commit
  5. Push
  6. Send Pull Request

It will work, but, there's one non-obvious thing: pull request, probably, won't be merged immediately and there are high chances, that there will be some commits commits pushed to official repository before our commit is merged. As the result, we have a conflict between upstream repository and our forked repository. So, at this point, if we plan to use our forked repository again, we have to a€omergea€¯ upstream changes. It's not end of the world, and git, if we're lucky enough, will do this merge automatically as part of a€ogit pull upstreama€o, but it won't be a€ofast-forwarda€¯ merge and github's a€oNetworka€¯ diagram (or branches diagram in your favourite git GUI) won't be nice and clean anymore.

There's better approach, and it's name is a€oFeature Branchesa€o. It's quite simple. Once you have cloned forked repository (i.e. after steps 1-2), use a€ogit checkout -b new_branch_namea€¯ command (it's nice, if a€onew_branch_namea€¯ summarises changes you're going to implement). This command creates new branch, starting from current a€omastera€¯ and makes it active. Make changes and commit them: a€omastera€¯ branch is left intact and all the things you changed sit nicely in this new branch. Now, push these changes with a€ogit push origin new_branch_namea€¯ command. This will send your new branch to github. Now, open this branch on github and send Pull Request from your a€onew_branch_namea€¯ to upstream's a€omastera€¯, as usually.

Use a€ogit checkout mastera€¯ to return to the a€omastera€¯ branch, and, whenever upstream merges your changes, and you pull those, they will appear here automatically, without a single a€omergea€¯ effort from you. As a bonus, you get beautiful tree of branches without complex knots.