If you're an open source maintainer, or just an interested developer that wants to try out the changes in a pull request submitted by another contributor, it is now incredibly simple.
Auto setup remotes
The first thing to do is turn on
autoSetupRemote, a feature introduced in Git 2.37.0.
If your version of Git is lower than this, upgrade by following the official install instructions or running
brew install git (for Homebrew users).
Once you've upgraded, run the following command to turn
git config --global --add --bool push.autoSetupRemote true
Using GitHub CLI
If you aren't already using the GitHub CLI
gh, install it!
Then, from your local clone of the public repo, run:
gh pr checkout 2896
It'll setup a new remote and checkout the branch automatically -- you can pull any new commits with
git pull, and even push up commits to the branch (if you're a maintainer and the appropriate checkbox is checked in the pull request).
This is not nearly as nice, but if you can't use the GitHub CLI, it is an option.
For your own repo, run (one time):
git config --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*"
Or, if it's a public repo and you are forking it, use upstream instead:
git config --add remote.upstream.fetch "+refs/pull/*/head:refs/remotes/upstream/pr/*"
git pull will automatically pull down the pull request branches, and you can check them out like:
git checkout pr/2896
This does not automatically set up remotes for you, so you can't interact directly with the other contributor's remote (like pushing commits to it) unless you set up the remote separately.
If you prefer a GUI for Git, then you likely use this feature already, as there are lots of tools that offer one-click pull request checkouts: VSCode Git integration, GitKraken, and the GitHub Desktop app, for example. If that sounds more your speed, try one out!