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

GitGithubPull RequestGit Checkout

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

This will fetch without you having to name a branch:

git pull origin pull/939/head

https://stackoverflow.com/q/15397059/#32447815

Solution 3 - Git

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

I hope it helps.

Solution 4 - Git

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"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@github.com:joyent/node.git
    fetch = +refs/pull/*/head:refs/remotes/origin/pr/*

> 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'

Solution 5 - Git

Github recently released a cli utility called github-cli. After installing it, you can checkout a pull request's branch locally by using its id (doc)

e.g: gh pr checkout 2267

This works with forks as well but if you then need to push back to the fork, you'll need to add the remote repository and use traditional git push (until this ticket gets implemented in gh utility)

Solution 6 - Git

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

Solution 7 - Git

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.

Solution 8 - Git

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

Solution 9 - Git

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

Solution 10 - Git

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.

Solution 11 - Git

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

Solution 12 - Git

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

Solution 13 - Git

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

Solution 14 - Git

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

Solution 15 - Git

With newer versions of Git:

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

Solution 16 - Git

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

Solution 17 - Git

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

Solution 18 - Git

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 -

Solution 19 - Git

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

Solution 20 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGarfieldKlonView Question on Stackoverflow
Solution 1 - GittimboView Answer on Stackoverflow
Solution 2 - GitZomboView Answer on Stackoverflow
Solution 3 - GitAndrew YmazView Answer on Stackoverflow
Solution 4 - GitVonCView Answer on Stackoverflow
Solution 5 - GitbagerardView Answer on Stackoverflow
Solution 6 - GitAmitBView Answer on Stackoverflow
Solution 7 - Gitgabbar0xView Answer on Stackoverflow
Solution 8 - GitAriel GabizonView Answer on Stackoverflow
Solution 9 - GitTomas TomecekView Answer on Stackoverflow
Solution 10 - Gitosexp2003View Answer on Stackoverflow
Solution 11 - Gitinit0View Answer on Stackoverflow
Solution 12 - GitDaniel WehnerView Answer on Stackoverflow
Solution 13 - GitJohannes EwaldView Answer on Stackoverflow
Solution 14 - GitElakyaView Answer on Stackoverflow
Solution 15 - GitA. GilleView Answer on Stackoverflow
Solution 16 - GitPankajAView Answer on Stackoverflow
Solution 17 - GitSumerView Answer on Stackoverflow
Solution 18 - GitGorvGoylView Answer on Stackoverflow
Solution 19 - GitShashanka BalakuntalaView Answer on Stackoverflow
Solution 20 - GitFatih BulutView Answer on Stackoverflow