All Articles

How to checkout to a pull request in GitHub

There was a pull request on GitHub and it contains a feature I wanted to test. I didn’t know how to test it locally. Should I just copy the changes to my local since the changes weren’t that big? But what if it was?

Luckily, I found some answers by googling and decided to write it up for future reference.

Git provides a command for it and here is the sample syntax

$ git fetch <remote> pull/:id/head:<branch>

Here is an example demo:

$ git remote -v
origin (fetch)
origin (push)
upstream (fetch)
upstream (push)

$ git fetch upstream pull/123/head:testing-pr
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Total 17 (delta 15), reused 15 (delta 15), pack-reused 2
Unpacking objects: 100% (17/17), 1.78 KiB | 58.00 KiB/s, done.
 * [new ref]         refs/pull/123/head -> testing-pr

$ git checkout testing-pr
  • The value I used for <remote> is upstream because that’s where the pull request resides
  • It will create a branch named testing-pr locally so this doesn’t have to be the exact name of the author’s branch.

And you can now test the pull request locally. That’s it and I hope it helps!