Checkout pull requests from github locally

2022-03-20 17:45:00 +0100 - Written by Mikal Villa

Sometimes for various reasons I want to get one or more pull requests from github to my local copy of the repository. In the beginning I went as far as checking out the repository of whoever wrote the pull request, if small enough copy paste when it’s only for quick tests. Luckly that’s past, there is better ways of doing it!

In .gitconfig add the following:

[remote "origin"]
  fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

Testing with a random golang project found on github the results are like this (I checked it out some days ago, and is now updaing my local copy):

❯ git fetch
remote: Enumerating objects: 66, done.
remote: Counting objects: 100% (66/66), done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 66 (delta 48), reused 53 (delta 45), pack-reused 0
Unpacking objects: 100% (66/66), 334.02 KiB | 195.00 KiB/s, done.
   dc0d081b..77858586  master              -> origin/master
 * [new ref]           refs/pull/4387/head -> origin/pr/4387
 * [new ref]           refs/pull/4400/head -> origin/pr/4400
 * [new ref]           refs/pull/4403/head -> origin/pr/4403
 * [new ref]           refs/pull/4406/head -> origin/pr/4406
 * [new ref]           refs/pull/4411/head -> origin/pr/4411
 * [new branch]        qq-rule             -> origin/qq-rule

Then you can just do git checkout origin/pr/4387 and there it is, at localhost :)