Last Updated: June 28, 2016
· filosottile

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.

6 Responses
Add your response

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 ·

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

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 ·

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

over 1 year ago ·

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

over 1 year ago ·

You can also check out this tip from GitHub on doing a similar thing.

The 'hub' CLI tool from GitHub can also help.

over 1 year ago ·