Github PRs

Fetching Github Pull Requests easily

Github has got a nice and handy feature with pull-requests, you all know this. But how do you fetch them fast and without any effort?

Github uses a specific “namespace” to store the refs of pull-requests. Any normal git-client won’t fetch it, so the repo stays clean for a normal user. We have to modify the git-client.

git config --add remote.origin.fetch "+refs/pull/*:refs/pull/*"

This gives you refs like:

refs/pull/123/head  ->  the HEAD commit of the PR to merge
refs/pull/123/merge ->  the commit of the merged branch
                        (when merged to e.g. master)

Let’s modify this a bit. IMO this gives me too much information.

Do I need the merge commit? I don’t need it.

$ git config --add remote.origin.fetch "+refs/pull/*/head:refs/pull/*"
$ git checkout refs/pull/123

What do I do, if I’ve got multiple repositories, where PRs are happening?

This can be handy, if you commit into an organisation repository, but have your own fork, where others may create PRs, too.

Just add the remote-name to your local refs:

$ git config --add remote.<remote>.fetch "+refs/pull/*/head:refs/pull/<remote>/*"
$ git checkout refs/pull/<remote>/123

I don’t want to type ref/.. at first. This is maybe a hack, but I’ve done it 😀

$ git config --add remote.<remote>.fetch "refs/pull/*/head:refs/remotes/<remote>/pull/*"
$ git checkout <remote>/pull/123

This is now, like you would reference any other branch from the remote-repository.

Make the configuration globally available.

Even with no added remotes, you’ll have instantly the upstream and origin remotes.

TL;DR

Do this for your repo, where you want to fetch PRs, too:

$ cd path/to/repo
$ git config --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pull/*"
$ git fetch
$ git checkout origin/pull/123

Now you’re on the head commit of the PR with ID 123, you can test it now on your own.