Check Out a Pull Request by Number

Pull requests on GitHub can come from the same repo, or they can come from a fork. If they come from the same repo, then they can easily be checked out locally:

$ git checkout cool-feature

But what if the pull request comes from a fork? It’s a bit laborious to check it out locally:

$ git remote add someone-else git@github.com:someone-else/foobar.git
$ git fetch someone-else
$ git checkout someone-else/neat-feature

You can avoid the need to add a new remote by checking out a pull request by number, as its own branch in your local repo. Assuming that you want to check out PR #50:

$ git fetch origin pull/50/head:pr/50
$ git checkout pr/50

Voila! No need for a new remote, since GitHub turns pull requests into pseudo-branches. Note that this will work with any pull request, even ones that aren’t from a fork.

If you’d like this as a git alias, add the following to your ~/.gitconfig in the [alias] section:

co-pr = !sh -c 'git fetch origin pull/$1/head:pr/$1 && git checkout pr/$1' -

(You can see it in my .gitconfig here).

Now you can run git co-pr 50 any time!