Today I’m talking about the merge button on GitHub, why you shouldn’t use it and presenting an alternative which is cleaner & easier to use.

Screenshot

In the recent months, I’ve found myself publishing more and more open source projects onto GitHub. Whenever a pull request comes in, it’s often really easy to just hit Merge pull request and move on with my day. The problem with this approach is that it introduces a number of individual merge commits into the history which I find to be rather unsightly and distracting.

Screenshot

Also, when someone submits a pull request to resolve a bug or add a feature, it may include a number of different commits for the same issue. I always prefer my pull requests to be squashed into a single commit before I add them to my upstream branch. If you use the merge button, you don’t have the option to do this and every commit will be individually added to the upstream.

Screenshot

Finally, if you want to test someone’s pull request you have to go through the rigmarole of checking out their branch locally, testing it and suggesting any updates.

The solution?

GitHub provide a small utility called hub which adds a number of extensions to the Git binary to improve your workflow while using GitHub. The hub library extends the am method to support applying the contents of a pull request onto your current working copy. Here’s how it works…

  1. Find the HTTP of the pull request you want to merge. It’ll look something like http://github.com/adamcooke/authie/pull/4.

  2. Pull the latest copy of your repository and checkout the branch you want to merge the pull request into.

  3. Run hub am followed by the URL to the pull request you found in step 1.

  4. You’ll now find that the commit(s) from the pull request have now been applied to your branch.

  5. If you simply want to keep all these commits, just test and then run enter git push to add them to your upstream repository.

Once you have pushed, you’ll see that the commits exist with the original author’s name plus your name as the person who actually committed them to the repository. No merge commits.

Screenshot

Squashing into a single commit

If you have multiple commits and you’d like to squash them into a single commit, you can use rebasing to squash them together.

Follow all the steps above but before you push to master, you need to do the following:

  1. Run git rebase -i origin/master (replacing origin and master with the name of the upstream version of your repository).

  2. You will now see a complete list of commits which exist on your local branch but have not been pushed to your upstream. This should only include those which you have just added from your pull request.

  3. Change the keyword of the first commit to edit and the keyword of all the remaining commits to squash. Close the file to continue.

  4. The commits will now be squashed together and you will be given the opportunity to edit the commit message for the commit. You may wish to also add closes #1234 (where 1234 is the number of the pull reuqest) so that it is automatically closed when you push to GitHub.

Throughout all of that, the original author will be maintained. You can of course explore git rebase in more detail if you want to make other changes.

Tell us how you feel about this post?