Quickly applying GitHub pull requests

Applying GitHub pull requests can be very easy if the patch is obviously good -- you just click the "Merge pull request" button and get on with your day. If you need to get the code into a local repository before you can push it up to your project's master branch (as I always do) things are a little more involved.

The old way

The GitHub docs on dealing with pull requests are excellent. They point out that you can add the contributor's fork as a remote, checkout a new branch and do your thing:

$ git checkout master
$ git remote add kneath git://github.com/kneath/jobs.git
$ git fetch kneath
$ git merge kneath/error-page
$ git push origin master

And that's what I used to do. A bit of a chore.

The quick way

Again, from the GitHub docs, we see we can do this:

$ git checkout master
$ curl http://github.com/github/jobs/pull/25.patch | git am
$ git push origin master

Well that's brilliant. I often use git format-patch to shuffle commits around between machines but hadn't thought of using it like this. I don't want to remember the URL though.

Time for some bash-fu.

$ git checkout master
$ pull-request-patch 25 | git am
$ git push origin master

You can download pull-request-patch from my gma-utils project on GitHub.

Add something like this (specifying your own GitHub username) to your shell's startup file (e.g. ~/.bashrc):

export GITHUB_USER=gma

You're welcome.


There may well be lots of scripts that help you do this, and I just stumbled on an alternative approach (on the same day, would you believe, that I wrote my little script). It's available on GitHub as git-pull-request, or you can read the blog post that announced it on splitbrain.org.

And then there's John Resig's pulley. It serves a slightly different, but highly related purpose. I recommend you take a look at that too.

I love feedback and questions — please feel free to get in touch on Mastodon or Twitter, or leave a comment.

Published on in Git