How can I check out a GitHub pull request with git?
Asked Answered
H

21

550

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?

Handhold answered 19/12, 2014 at 14:14 Comment(5)
What does the complete remote section of your config file look like?Minimum
possible duplicate of How can I fetch an unmerged pull request for a branch I don't own?Iverson
possible duplicate of github clone from pull request?Aspergillus
I ended up on this question, but I actually needed https://mcmap.net/q/12192/-how-do-i-check-out-a-remote-git-branch/2413303Lineage
The second paragraph is not a complete sentence. "But when I add ... and do a git fetch" - when you do these things what happens?Odin
F
862

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.

Flatus answered 1/6, 2015 at 23:25 Comment(12)
I used this to fetch a pr from an upstream repo into my local forked repo, you can replace origin with upstream as well.Ejaculatory
My command ended up looking like git fetch origin pull/1/head:githubusername , not what I was expectingHarm
@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.Mckinley
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.Bookbinding
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.Workmanlike
Just one question please, how to fetch new-coming commits to this local-created branch?Lashonlashond
Was able to solve the issue using the GitHub Documentation link posted by @FlatusYore
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"Larsen
Also note that "head" needs to be in small letters, not the usual caps HEAD.Munich
you can also omit branch name and then do git checkout FETCH_HEADUrsulaursulette
How does one pull PR updates if the author commits changes to the same PR?Epoch
Where are the changes? git status shows none of the changes in the PR, and git pull says everything is up to date.Portulaca
U
204

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?

Undershoot answered 19/12, 2014 at 14:14 Comment(3)
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.Gonzales
This also works if you later want to pull changes from the pull request into your local branch.Gerrigerrie
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 branchIbex
S
114

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

Surroundings answered 30/8, 2017 at 19:4 Comment(4)
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).Olivas
Voted! No need for a local branch and can update from remote easily.Lashonlashond
@JamesRao thanks, no local branch is the best part!Surroundings
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?Carlow
S
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'
Signesignet answered 19/12, 2014 at 14:21 Comment(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).Volost
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.Dulcy
@Dulcy Thank you for your feedback. I have edited the answer to include your comment for more visibility.Signesignet
V
52

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.

Vallery answered 2/3, 2020 at 20:49 Comment(0)
O
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

Overcompensation answered 27/2, 2017 at 16:14 Comment(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!Salim
@Salim 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.Rheinland
E
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.

Ernest answered 12/6, 2017 at 12:16 Comment(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.Kirshbaum
F
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.

Finical answered 6/4, 2018 at 7:40 Comment(0)
H
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

Hamford answered 15/4, 2017 at 21:57 Comment(1)
I hit exactly this problem with the git fetch origin pull/ID/head:BRANCHNAME approach mentioned in https://mcmap.net/q/73449/-how-can-i-check-out-a-github-pull-request-with-git. Thanks for the solution!Scary
A
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
Aeriform answered 4/7, 2017 at 6:52 Comment(1)
What about pushing it back upstream for updates?Mindimindless
M
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
Math answered 2/4, 2018 at 18:57 Comment(0)
S
8

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

Shocking answered 1/6, 2022 at 16:24 Comment(0)
I
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
Inkstand answered 19/2, 2019 at 8:7 Comment(0)
P
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
Purvey answered 24/9, 2015 at 14:22 Comment(0)
S
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

Siamese answered 11/2, 2020 at 8:29 Comment(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.Never
A
4

With newer versions of Git:

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

Ankylosaur answered 5/8, 2021 at 15:8 Comment(0)
W
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
Westlund answered 15/9, 2021 at 8:8 Comment(0)
H
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

Heirdom answered 17/2, 2020 at 10:49 Comment(1)
When you shared code, please try to explain your codeKeystone
T
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 -

Trail answered 22/4, 2021 at 13:0 Comment(0)
T
-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

Ted answered 8/1, 2021 at 7:28 Comment(1)
How is this answer different from bagerard's?Gilroy
M
-2

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
Metamorphic answered 28/1, 2021 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.