Checking out GitHub pull requests to a local branch

After some trial and error, here’s how I check out Github pull requests to a local branch with tracking.

Edit .git/config, add the following line in the section for the desired remote.

fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

Ensure to add it before the existing line for remote branches, e.g.

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

Run git fetch and see all the new remote “branches” appear.

Now you can check out a PR branch: git checkout pr/123

Even better, you can create a worktree for each PR you want to review:

git worktree add pr-123 pr/123
cd pr-123
# review review review
cd ..
git worktree remove pr-123

This also allows you to track the remote ref, so if the PR receives additional commits you can just pull them down.