555

I'd like to check out a previously created pull request (created via GitHub web interface). I searched and found different places where a refs/pull or refs/pull/pr

But when I add fetch = +refs/pull/*/head:refs/remotes/origin/pr/* to the git config file and do a git fetch

What I'm doing wrong? Should GitHub create automatically the pull/xyz stuff, or do I have to configure something?

5

21 Answers 21

869

To fetch a remote PR into your local repo,

git fetch origin pull/$ID/head:$BRANCHNAME

where $ID is the pull request id and $BRANCHNAME is the name of the new branch that you want to create. Once you have created the branch, then simply

git checkout $BRANCHNAME

For instance, let's imagine you want to checkout pull request #2 from the origin main branch:

git fetch origin pull/2/head:MASTER

See the official GitHub documentation for more.

12
  • 30
    I used this to fetch a pr from an upstream repo into my local forked repo, you can replace origin with upstream as well.
    – Jngai1297
    Commented Feb 17, 2016 at 22:33
  • 36
    My command ended up looking like git fetch origin pull/1/head:githubusername , not what I was expecting
    – Anthony
    Commented Mar 13, 2016 at 23:20
  • 22
    @Antoine BRANCHNAME is whatever you want to name the branch. I'm guessing you tried to use a name that already existed (e.g. master) and that didn't work, so you tried your username, which did work, because their was no branch with that name. Perhaps I misunderstand what you were saying.
    – Nateowami
    Commented Jun 20, 2018 at 18:18
  • 4
    It may be the case that you configured your local repo the way that origin points to your fork and upstream – to the original repository (following help.github.com/articles/configuring-a-remote-for-a-fork, for instance). Make sure to change origin to upstream in the mentioned command, if you want to fetch pull request from the original repo.
    – mvlabat
    Commented Nov 29, 2018 at 12:19
  • 4
    I was trying to fetch including the "#" symbol in the ID, as highlighted in GitHubs documentation, as in pull/#1/head:branch. Only the number must go in, so pull/1/head/branch.
    – Lucas
    Commented Apr 13, 2020 at 6:25
212

This will fetch without you having to name a branch:

git pull origin pull/939/head

How do I get a specific pull request on my machine?

3
  • 58
    Note that if you do this while on your master branch, for example, it will commit directly into this branch. If you'd like to bring the pull request into a separate branch for staging, try @timbo's answer.
    – johnthagen
    Commented Jan 30, 2017 at 13:16
  • 3
    This also works if you later want to pull changes from the pull request into your local branch.
    – luator
    Commented Jan 24, 2018 at 13:58
  • 1
    I was looking for this only, as I need to pull others code to review and I don't want to switch to a new branch Commented Oct 11, 2021 at 7:43
121

I prefer to fetch and checkout without creating a local branch and to be in HEAD detached state. It allows me quickly to check the pull request without polluting my local machine with unnecessary local branches.

git fetch upstream pull/ID/head && git checkout FETCH_HEAD

where ID is a pull request ID and upstream where is original pull request has been created (it could be origin, for example).

4
  • 7
    I like this solution. One of the benefits is that if the PR is updated with more commits, then you can just run this again and it'll pull in the new commit(s). Commented Jul 21, 2019 at 21:16
  • 1
    Voted! No need for a local branch and can update from remote easily.
    – James Rao
    Commented Mar 24, 2021 at 0:36
  • 1
    @JamesRao thanks, no local branch is the best part! Commented Mar 24, 2021 at 13:28
  • Thanks, is it true, I guess, that before git-gc, and even after checkout or switch to the original branch, there may still be some unconnected commits in the local repository?
    – leonbear
    Commented Oct 13, 2022 at 2:51
69

That gist does describe what happend when you do a git fetch:

Obviously, change the github url to match your project's URL. It ends up looking like this:

[remote "origin"]
    url = [email protected]:joyent/node.git
    fetch = +refs/pull/*/head:refs/remotes/origin/pr/*
    fetch = +refs/heads/*:refs/remotes/origin/*

Note the order of fetch refspecs, as suggested in the comments by crashneb, in his own's answer.

If not, meaning if you don't have the right order because of a:

git config --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*" ... 

and then trusted the PR-checkout to naturally setup the new local branch with something like git switch pr/1 -- then you might have trouble should the PR get updated and you want to just git pull it again.
The branch.pr/1.merge config value will not be correct.


Now fetch all the pull requests:

$ git fetch origin
From github.com:joyent/node
 * [new ref]         refs/pull/1000/head -> origin/pr/1000
 * [new ref]         refs/pull/1002/head -> origin/pr/1002
 * [new ref]         refs/pull/1004/head -> origin/pr/1004
 * [new ref]         refs/pull/1009/head -> origin/pr/1009
...

To check out a particular pull request:

$ git checkout pr/999
Branch pr/999 set up to track remote branch pr/999 from origin.
Switched to a new branch 'pr/999'

You have various scripts listed in issues 259 to automate that task.
The git-extras project proposes the command git-pr (implemented in PR 262)

git-pr(1) -- Checks out a pull request locally

SYNOPSIS

git-pr <number> [<remote>]
git-pr clean

DESCRIPTION

Creates a local branch based on a GitHub pull request number, and switch to that branch afterwards.

The name of the remote to fetch from. Defaults to origin.

EXAMPLES

This checks out the pull request 226 from origin:

$ git pr 226

remote: Counting objects: 12, done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 12 (delta 3), reused 9 (delta 3)
Unpacking objects: 100% (12/12), done.
From https://github.com/visionmedia/git-extras
  * [new ref] refs/pull/226/head -> pr/226
Switched to branch 'pr/226'
3
  • I was not aware of the git-extras project, thanks for mentioning it! − See also this other SO answer How do I checkout a PR from a fork? where I proposed a similar command git prw that additionally sets the upstream branch (so the branch so obtained is not a "read-only" branch).
    – ErikMD
    Commented Apr 30, 2021 at 22:31
  • If you adjusted your config as prescribed by this answer, perhaps with something like git config --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*" ... and then trusted the PR-checkout to naturally setup the new local branch with something like git checkout pr/1 -- then you will have trouble trying to just git pull it again to get PR updates. The branch.pr/1.merge config value will not be correct. See here for how to address that issue.
    – CrashNeb
    Commented Sep 13, 2022 at 21:19
  • @CrashNeb Thank you for your feedback. I have edited the answer to include your comment for more visibility.
    – VonC
    Commented Sep 13, 2022 at 21:23
53

The Github CLI gh allows you to checkout a pull request's branch locally by its id (doc)

gh pr checkout 2267

This allows pushing changes back to the fork as well with a simple git push if the PR submitter has given you edit rights to their fork.

0
29

If you are using Github.com, go to "Pull requests", click on the relevant pull request, and then click on the "command line instructions" link: command line instructions at Github.com

2
  • Say, is there actually a way - when you are looking at github.com I mean - to download the new/changed files, of the PR? So, when you are looking at a repo on github, you can click the handy "download as a zip" button, or indeed, you can, quite simply, click through and just look at each (entire) file of the project. For PR, I can't see how to, simply, click through to "look at the file" - know what I mean? Am I missing something? Cheers!
    – Fattie
    Commented Apr 7, 2017 at 13:27
  • 1
    @Fattie Yes. You simply click yourself through to your contributor's repository (you can pick any state of their repository, the latest or any other commit) and then use the ↓ CodeDownload ZIP feature on the repository's main page.
    – Peterino
    Commented Oct 25, 2020 at 4:33
22

Referencing Steven Penny's answer, it's best to create a test branch and test the PR. So here's what you would do.

  1. Create a test branch to merge the PR into locally. Assuming you're on the master branch:

git checkout -b test

  1. Get the PR changes into the test branch

git pull origin pull/939/head:test

Now, you can safely test the changes on this local test branch (in this case, named test) and once you're satisfied, can merge it as usual from GitHub.

1
  • 1
    I'd go even one better - I would create a worktree, set it to a new test branch and THEN pull in the PR - that way I don't need about restoring branches locally when done; I just dispose of the worktree. In fact I NEVER just checkout -b anymore - I always create a worktree and then branch. Disk is cheap. Of course, I have a script that does this; I don't type all the command needed individually.
    – mpersico
    Commented Nov 15, 2018 at 18:15
12

The problem with some of options above, is that if someone pushes more commits to the PR after opening the PR, they won't give you the most updated version. For me what worked best is - go to the PR, and press 'Commits', scroll to the bottom to see the most recent commit hash enter image description here and then simply use git checkout, i.e.

git checkout <commit number>

in the above example

git checkout 0ba1a50

1
12

For Bitbucket, you need replace the word pull to pull-requests.

First, you can confirm the pull request URL style by git ls-remote origin command.

$ git ls-remote origin |grep pull
f3f40f2ca9509368c959b0b13729dc0ae2fbf2ae    refs/pull-requests/1503/from
da4666bd91eabcc6f2c214e0bbd99d543d94767e    refs/pull-requests/1503/merge
...

As you can see, it is refs/pull-requests/1503/from instead of refs/pull/1503/from

Then you can use the commands of any of the answers.

11

You can use git config command to write a new rule to .git/config to fetch pull requests from the repository:

$ git config --local --add remote.origin.fetch '+refs/pull/*/head:refs/remotes/origin/pr/*'

And then just:

$ git fetch origin
Fetching origin
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 4 (delta 2), reused 4 (delta 2), pack-reused 0
Unpacking objects: 100% (4/4), done.
From https://github.com/container-images/memcached
 * [new ref]         refs/pull/2/head -> origin/pr/2
 * [new ref]         refs/pull/3/head -> origin/pr/3
1
  • 2
    What about pushing it back upstream for updates? Commented Apr 12, 2022 at 20:40
9

Although most of the answers on this thread work, I prefer to fetch a pull request in a new branch and do a soft reset to an old commit (moves the PR changes to the staging area), this allows me to test the PR changes as well as see the difference in my IDE.

git fetch origin pull/<PR-id>/head:<BRANCH_NAME>
git checkout BRANCH_NAME

and then do a soft reset to a old commit (see list of commits using git log)

git reset --soft <hash_of_old_commit>

e.g

git fetch origin pull/65/head:test-branch 
git checkout test-branch
git log # press 'q' to exit
git reset --soft 7d7fe166cd878ed70c559c4e98faf2323532

Running the above command will pull the changes of the PR and show them in your IDE version and they won't be committed and you can see the difference in your staging area (as if those changes were made locally.)

Ref: Github docs reference

8

If you're following the "github fork" workflow, where you create a fork and add the remote upstream repo:

14:47 $ git remote -v
origin  [email protected]:<yourname>/<repo_name>.git (fetch)
origin  [email protected]:<yourname>/<repo_name>.git (push)
upstream        [email protected]:<repo_owrer>/<repo_name>.git (fetch)
upstream        [email protected]:<repo_owner>/<repo_name>.git (push)

to pull into your current branch your command would look like:

git pull upstream pull/<pull_request_number>/head

to pull into a new branch the code would look like:

git fetch upstream pull/<pull_request_number>/head:newbranch
6

I'm using hub, a tool from github: https://github.com/github/hub

With hub checking out a pull request locally is kinda easy:

hub checkout https://github.com/owner/repo/pull/1234
or
hub pr checkout 1234
5

With newer versions of Git:

git fetch origin refs/pull-requests/<id>/from:<localbranchname>

4

I accidentally ended up writing almost the same as provided by git-extras. So if you prefer a single custom command instead of installing a bunch of other extra commands, just place this git-pr file somewhere in your $PATH and then you can just write:

git pr 42
// or
git pr upstream 42
// or
git pr https://github.com/peerigon/phridge/pull/1
4

Get the remote PR branch into local branch:

git fetch origin ‘remote_branch’:‘local_branch_name’

Set the upstream of local branch to remote branch.

git branch --set-upstream-to=origin/PR_Branch_Name local_branch

When you want to push the local changes to PR branch again

git push origin HEAD:remote_PR_Branch_name

1
  • 1
    This should note that on Github, you can't actually edit the PR and push it back. The remote refs/pull/ namespace is read-only.
    – K.S.
    Commented Dec 1, 2020 at 0:13
2

To quickly check PR on your local, open it and check the branch name from which PR is created.

enter image description here As we can see in the red line above name of branch is 'CLUPET-173-glrr-apis' use below command to quickly see the code in the PR/Branch

git checkout origin/CLUPET-173-glrr-apis

Now this code on your local would run as detached head mode.

To stop all PR code viewing and to go back to your previous branch

git switch -

In case you want to move PR(plus any new local changes you made after fetching PR) to a new local branch use below command

git switch -c myNewLocalBranch
1

Suppose your origin and upstream info is like below

   $ git remote -v
   origin  [email protected]:<yourname>/<repo_name>.git (fetch)
   origin  [email protected]:<yourname>/<repo_name>.git (push)
   upstream   [email protected]:<repo_owner>/<repo_name>.git (fetch)
   upstream   [email protected]:<repo_owner>/<repo_name>.git (push)

and your branch name is like

   <repo_owner>:<BranchName>

then

   git pull origin <BranchName>

shall do the job

1
  • When you shared code, please try to explain your code Commented Feb 17, 2020 at 11:12
1

To checkout PR and see all changes from that PR as compared to the main branch in VSCode. Similar to files changed section of Github PR page.

  1. checkout PR (100) in 'detached HEAD' state
    git fetch origin pull/100/head && git checkout FETCH_HEAD

  2. show as uncommitted changes
    git reset main

  3. switch back to main branch and carry these changes
    git switch -

-1

Create a local branch

git checkout -b local-branch-name

Pull the remote PR

git pull [email protected]:your-repo-ssh.git remote-branch-name
-2

There is an easy way for doing this using git-cli

gh pr checkout {<number> | <url> | <branch>}

Reference: https://cli.github.com/manual/gh_pr_checkout

1
  • 1
    How is this answer different from bagerard's? Commented Feb 11, 2021 at 10:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.