z5rkga
Last Updated: June 28, 2016
·
12.97K
· filosottile
A336b0fe148e72a16744805d059f812a

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.

Say Thanks
Respond

6 Responses
Add your response

2932
B1f557a37ffc97d9387c0962160562c5

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?

over 1 year ago ·
2935
A336b0fe148e72a16744805d059f812a

@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.

over 1 year ago ·
2938
B1f557a37ffc97d9387c0962160562c5

The details of the refspec are in Chapter 9.5 of @schacon's book ProGit. Neat work on trawling the Travis logs! ;)

over 1 year ago ·
5248
B1f557a37ffc97d9387c0962160562c5

Just posted a followup to this: GitHub: add remote for pulls and merges

over 1 year ago ·
13401
F981b37f5120cd32282071b4e9de12bb

You don't need to specify the "refs/" part (anymore) pull/#/head works, too.

over 1 year ago ·
27818
None

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.

over 1 year ago ·
Filed Under