516

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?

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
GarfieldKlon
  • 11,170
  • 7
  • 31
  • 33
  • What does the complete remote section of your config file look like? –  Dec 19 '14 at 14:25
  • 5
    possible duplicate of [How can I fetch an unmerged pull request for a branch I don't own?](http://stackoverflow.com/questions/6743514/how-can-i-fetch-an-unmerged-pull-request-for-a-branch-i-dont-own) – Ciro Santilli OurBigBook.com Aug 16 '15 at 09:06
  • possible duplicate of [github clone from pull request?](http://stackoverflow.com/questions/14947789/github-clone-from-pull-request) – BuZZ-dEE Aug 31 '15 at 20:44
  • I ended up on this question, but I actually needed https://stackoverflow.com/q/1783405/2413303 – EpicPandaForce Dec 01 '17 at 10:35
  • 3
    The second paragraph is not a complete sentence. "But when I add ... and do a git fetch" - when you do these things *what* happens? – cp.engr Jan 11 '18 at 21:55

21 Answers21

816

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.

iamtodor
  • 734
  • 8
  • 21
timbo
  • 13,244
  • 8
  • 51
  • 71
  • 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 Feb 17 '16 at 22:33
  • 35
    My command ended up looking like `git fetch origin pull/1/head:githubusername` , not what I was expecting – Anthony Mar 13 '16 at 23:20
  • 20
    @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 Jun 20 '18 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 https://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 Nov 29 '18 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 Apr 13 '20 at 06:25
  • 3
    Just one question please, how to fetch new-coming commits to this local-created branch? – James Rao Mar 09 '21 at 01:51
  • Was able to solve the issue using the [GitHub Documentation link](https://help.github.com/en/articles/checking-out-pull-requests-locally) posted by @timbo – Haddock-san Aug 09 '21 at 11:52
  • i suggest as an improvement to this post an example with content, e.g., if the pull request is #37 and the new branch I want to create is called "regressionFixes", then "git fetch origin pull/37/head:regressionFixes" And, another would be making it all one command (because it's easy to forget to switch branches); i.e., with above example: "git fetch origin pull/37/head:regressionFixes && git checkout regressionFixes" – Adamokkha Sep 09 '21 at 15:08
  • Also note that "head" needs to be in small letters, not the usual caps HEAD. – Waqar Dec 03 '21 at 10:47
  • you can also omit branch name and then do `git checkout FETCH_HEAD` – licancabur Dec 31 '21 at 09:40
  • How does one pull PR updates if the author commits changes to the same PR? – Devis L. Jan 31 '23 at 02:10
195

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?

Community
  • 1
  • 1
Zombo
  • 1
  • 62
  • 391
  • 407
  • 53
    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. – phoenix Jan 30 '17 at 13:16
  • 3
    This also works if you later want to pull changes from the pull request into your local branch. – luator Jan 24 '18 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 – Vivek Agrawal Oct 11 '21 at 07:43
99

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

tshepang
  • 12,111
  • 21
  • 91
  • 136
Andrew Ymaz
  • 1,863
  • 8
  • 12
  • 5
    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). – Alex Johnson Jul 21 '19 at 21:16
  • 1
    Voted! No need for a local branch and can update from remote easily. – James Rao Mar 24 '21 at 00:36
  • 1
    @JamesRao thanks, no local branch is the best part! – Andrew Ymaz Mar 24 '21 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 Oct 13 '22 at 02:51
68

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 = git@github.com: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'
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 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?](https://stackoverflow.com/a/62432946/9164010) 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 Apr 30 '21 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](https://stackoverflow.com/a/40828472/5844631) for how to address that issue. – CrashNeb Sep 13 '22 at 21:19
  • @CrashNeb Thank you for your feedback. I have edited the answer to include your comment for more visibility. – VonC Sep 13 '22 at 21:23
43

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.

Janosh
  • 3,392
  • 2
  • 27
  • 35
bagerard
  • 5,681
  • 3
  • 24
  • 48
27

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

AmitB
  • 724
  • 8
  • 5
  • 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 Apr 07 '17 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 *↓ Code* ➜ *Download ZIP* feature on the repository's main page. – Peterino Oct 25 '20 at 04:33
21

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.

gabbar0x
  • 4,046
  • 5
  • 31
  • 51
  • 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 Nov 15 '18 at 18:15
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.

osexp2000
  • 2,910
  • 30
  • 29
11

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

Ariel Gabizon
  • 2,851
  • 2
  • 17
  • 22
  • 2
    I hit exactly this problem with the `git fetch origin pull/ID/head:BRANCHNAME` approach mentioned in https://stackoverflow.com/a/30584951/659732. Thanks for the solution! – Joe Wicentowski Aug 01 '17 at 21:14
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
Tomas Tomecek
  • 6,226
  • 3
  • 30
  • 26
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  git@github.com:<yourname>/<repo_name>.git (fetch)
origin  git@github.com:<yourname>/<repo_name>.git (push)
upstream        git@github.com:<repo_owrer>/<repo_name>.git (fetch)
upstream        git@github.com:<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
init0
  • 89
  • 1
  • 4
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
Daniel Wehner
  • 2,159
  • 1
  • 18
  • 22
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
Johannes Ewald
  • 17,665
  • 5
  • 44
  • 39
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

Elakya
  • 41
  • 4
  • 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](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/checking-out-pull-requests-locally#modifying-an-inactive-pull-request-locally). – K.S. Dec 01 '20 at 00:13
4

With newer versions of Git:

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

A. Gille
  • 912
  • 6
  • 23
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
Sumer
  • 2,687
  • 24
  • 24
1

Suppose your origin and upstream info is like below

   $ git remote -v
   origin  git@github.com:<yourname>/<repo_name>.git (fetch)
   origin  git@github.com:<yourname>/<repo_name>.git (push)
   upstream   git@github.com:<repo_owner>/<repo_name>.git (fetch)
   upstream   git@github.com:<repo_owner>/<repo_name>.git (push)

and your branch name is like

   <repo_owner>:<BranchName>

then

   git pull origin <BranchName>

shall do the job

martyn
  • 676
  • 7
  • 17
PankajA
  • 11
  • 1
1

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 a old commit (moves the PR changes to staging area), this allows me to test 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

Mahesh Jamdade
  • 17,235
  • 8
  • 110
  • 131
0

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 -

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
-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

-2

Create a local branch

git checkout -b local-branch-name

Pull the remote PR

git pull git@github.com:your-repo-ssh.git remote-branch-name
Fatih Bulut
  • 2,385
  • 1
  • 15
  • 12