Edward Thomson

This is day 5 of my Git Tips and Tricks Advent Calendar. If you want to see the whole list of tips as they're published, see the index.

Reviewing pull requests is tough work: whether you're using GitHub or Azure Repos or another hosting provider, they all try their best to give you a powerful code review experience right in the browser. But no matter how good they are, for some changes you really need to get hands-on, to run the code, to step through it in a debugger to really grok it.

Thankfully, GitHub and Azure Repos both expose pull request branches so that you can just fetch them locally and work with them. Those branches are hidden from your git client by default, though, so you'll need to set up "fetch specs" in your configuration that include those branches on the remote.

There are a few ways you can do this: you could set up another fetch spec on your origin remote. In this case, you'll always download remote pull request branches locally whenever you run git fetch. But if you work on a busy project with a lot of contributors (and a lot of pull requests), this will get annoying fast, especially if you're on Windows where branch management is slow.

Instead, you can set up a new remote called "pr", so that you can fetch pull request branches only when you want. To create a new remote (with the same URL as your existing origin):

git remote add pr $(git config remote.origin.url)

Now you can change the fetch specs for this remote to include the pull request branches. The remote branch information differs slightly depending on your hosting provider.

GitHub:

git config remote.pr.fetch '+refs/pull/*/head:refs/remotes/pr/*'

Azure Repos:

git config remote.pr.fetch '+refs/pull/*/merge:refs/remotes/pr/*'

Now if you want to check out a pull request locally, you can fetch from the pr remote and check out a branch pr/nnnn (where nnnn is the pull request number.

For example, to check out pull request 1234:

git fetch pr
git checkout pr/1234

Bonus: if you do have a lot of pull request branches, you may not want to fetch them all. To fetch a single branch, you can use the remote's branch name. You can see that in the configuration we set earlier. Again, it differs depending on your hosting provider.

GitHub:

git fetch refs/pull/1234/head
git checkout pr/1234

Azure Repos:

git fetch refs/pull/1234/merge
git checkout pr/1234

Now you can build and debug the pull request locally and send feedback in the web portal.