Checking Out GitHub Pull Requests Locally4 September 2015 · Filed in Tutorial
In this post, I’m going to show you how to use the Git command-line to check out GitHub pull requests locally. I take absolutely no credit for this trick! I picked this up from this GitHub Gist, and merely wanted to share it here so that others would benefit.
The GitHub gist shows you how to modify the Git configuration for a particular repository so that when you run
git fetch it will fetch all the pull requests for that repository as well. This is handy, but what I personally found most helpful was a comment that showed the command to fetch a specific pull request. The command looks like this:
git fetch origin pull/1234/head:pr-1234
Let me break that command down a bit:
originin this case refers to the Git remote for this repository on GitHub. If you are using the fork-and-pull method of collaborating via Git and GitHub, then you will have multiple Git remotes—and the remote you want probably isn’t
origin. For example, if you want to fetch a pull request from the original (not forked) repository, you’d want to use the name that corresponds to the Git remote for the original repository (I use
upstreamin these cases).
pull/1234/headportion refers to the pull request on GitHub. Every pull request has a number assigned. If the pull request is #10, then the path would be
pull/10/head. If the pull request is #9182, then the path is
:pr-1234portion of the command points to the local branch into which you’d like the specified pull request to be fetched. This branch should not already exist. If you try to fetch into an existing branch, the command will fail, so specify the name of a branch you’d like created as part of the process.
Once you run the command and the fetch is successful, then you can switch to the branch with
git checkout pr-1234 (or whatever name you gave the new branch) and start reviewing the contents of the pull request. Very handy!