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
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?
@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.
The details of the refspec are in Chapter 9.5 of @schacon's book ProGit. Neat work on trawling the Travis logs! ;)
Just posted a followup to this: GitHub: add remote for pulls and merges
You don't need to specify the "refs/" part (anymore) pull/#/head
works, too.
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.