GitHub: checkout a pull request as a branch
When you receive a PR on GitHub it is boring to add a new remote for the submitter fork and to fetch his branch.
Fortunately, GH creates a remote ref for each PR, so you can use the following syntax to create a local branch that reflects the PR branch:
git fetch origin refs/pull/PR_NUMBER/head:LOCAL_BRANCH
Elegant and convenient!
For example:
git fetch origin refs/pull/611/head:pull_611
git checkout pull_611
Change /head
to /merge
to get the ref to the pull request merged with its intended target branch.
Written by Filippo Valsorda
Related protips
6 Responses
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/245/1554748128138581550.jpg)
Just a small quibble, you don't need to put the +
before refs
, i.e., you don't need to force pull the ref. Other than that, awesome tip; how did you come about this feature?
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/27092/a336b0fe148e72a16744805d059f812a.jpeg)
@dexterous Thanks, actually I didn't know what it was standing for. I discovered the /merge
ref by looking at a Travis build for a pull request. Then I guessed the /head
variant.
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/245/1554748128138581550.jpg)
The details of the refspec are in Chapter 9.5 of @schacon's book ProGit. Neat work on trawling the Travis logs! ;)
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/245/1554748128138581550.jpg)
Just posted a followup to this: GitHub: add remote for pulls and merges
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/101063/f981b37f5120cd32282071b4e9de12bb.jpeg)
You don't need to specify the "refs/" part (anymore) pull/#/head
works, too.
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/139556/none.jpg)
You can also check out this tip from GitHub on doing a similar thing. https://help.github.com/articles/checking-out-pull-requests-locally/
The 'hub' CLI tool from GitHub https://hub.github.com can also help.