197

I would like to clone a repository from GitHub. The problem is I don't want the main branch; I want the version in this unapproved pull request.

Is it possible for me to clone the pull request version instead of the main repository?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Fresheyeball
  • 29,567
  • 20
  • 102
  • 164

13 Answers13

177

The easiest way to do that is like this:

git fetch origin pull/2/head
git checkout -b pullrequest FETCH_HEAD

You will now be on a new branch that is on the state of the pull request.

You might want to set up an alias by running

git config --global alias.pr '!f() { git fetch -fu ${2:-origin} refs/pull/$1/head:pr/$1 && git checkout pr/$1; }; f'

Now you can checkout any PR by running git pr <pr_number>, or git pr <pr_number> <remote> if your github remote is not named origin.

Abhijeet Kasurde
  • 3,937
  • 1
  • 24
  • 33
Chronial
  • 66,706
  • 14
  • 93
  • 99
  • 62
    Better: `git fetch origin pull/<#>/head:` ([via](https://coderwall.com/p/z5rkga)) – schlamar Mar 11 '14 at 08:01
  • 9
    I found myself referencing this SO answer often, so I stuck this into my `.gitconfig` file under `[alias]`: `pr = "!f() { git fetch $1 pull/$2/head:pull_$2; git co pull_$2; }; f"`. That way I just type `git pr upstream 62` and next thing I know, I am on a new branch of PR #62 from upstream! If you always use `origin` you could hardcode it instead of the `$1`, but that switches around for me. – matt--- May 11 '17 at 17:50
  • 1
    @matt that assumes you have an alias for checkout named `co` – giraffesyo Feb 16 '19 at 12:53
  • How do I pull it when commits are added? `--set-upstream-to` doesn't seem to work. Is there a better way than `git fetch ... && git reset --hard FETCH_HEAD`? – nponeccop Jul 12 '21 at 12:32
  • This did not work for me here: https://github.com/cgustav/lite_rolling_switch/pull/15/commits so I went to the source of the pull request here: https://github.com/Elvis-Sarfo/lite_rolling_switch/tree/null-safety-support and cloned that instead. – mLstudent33 Oct 25 '21 at 03:50
  • fatal: couldn't find remote ref pull/1/head (tried to use this for another repository) – trueToastedCode Mar 22 '23 at 14:46
  • Thanks to your posts. My twist on this is the following `[alias]`: `pr = "!f() { if [ $# -gt 1 ] ; then REM="$1"; NUM="$2"; else REM="upstream"; NUM="$1"; fi; git fetch "$REM" "pull/${NUM}/head:pull_${NUM}"; git checkout "pull_${NUM}"; }; f"` which allows to `git pr 123` and `git pr somefork 456` in a bit more natural argument order than having the fork remote name in the end, as per original port. Matter of taste :) – Jim Klimov Jun 01 '23 at 10:30
114

You can clone the branch you want by using the -b option and for pull request:

git clone https://github.com/user_name/repo_name.git -b feature/pull_request_name dir_name

In your case, the branch you want to clone is the source branch of the pull request (feature/mongoose-support):

git clone https://github.com/berstend/frappe.git -b feature/mongoose-support ./mongoose-support
shilovk
  • 11,718
  • 17
  • 75
  • 74
inancsevinc
  • 2,632
  • 2
  • 17
  • 9
53
git fetch origin refs/pull/PR_NUMBER/head:NEW_LOCAL_BRANCH

eg:

git fetch origin pull/611/head:pull_611
git checkout pull_611

Make changes, commit them, PUSH and open new PR from your fork on GitHub

Zombo
  • 1
  • 62
  • 391
  • 407
wierzbiks
  • 1,148
  • 10
  • 10
  • how can i merge this branches locally? i just cloned and fetched an unmerged pull request as above you did.And tried checkout branchname.But no changes appear in my IDE/text editor. – erginduran May 17 '17 at 13:19
21
git clone git://github.com/dweldon/frappe
cd frappe
git pull origin pull/2/head

How can I fetch an unmerged pull request for a branch I don't own?

Community
  • 1
  • 1
Zombo
  • 1
  • 62
  • 391
  • 407
  • 4
    Note `git pull` creates a merge into current branch; usually for a PR you'd want to just `git fetch` to get original author's code (it's then accessible as FETCH_HEAD). If you do want a merge, it's worth also mentioning `pull/2/merge` (instead of `pull/2/head`) — this makes GitHub give you the exact merge commit that would happen if you clicked [Merge] button now. – Beni Cherniavsky-Paskin Oct 18 '17 at 19:17
18

You could follow the directions in this gist to be able to check out the remote directly without having to figure out their repository and branch.

Example usage

For one of my projects (github3.py) I have the following in my github3.py/.git/config

[remote "github"]
    fetch = +refs/heads/*:refs/remotes/github/*
    fetch = +refs/pull/*/head:refs/remotes/github/pr/*
    url = git@github.com:sigmavirus24/github3.py

The first line is what is standard for every remote with the exception that github is replaced by the remote's name. What this means is that remote heads (or the heads of branches on that server) are "mapped" to local remotes prefixed by github/. So if I did git fetch github and had a branch on GitHub that wasn't already noticed locally on my machine, it would download the branch and I could switch to it like so: git checkout -t github/branch_name.

The second line does the same thing, but it does it for pull requests instead of standard git branches. That's why you see refs/pull/*/head. It fetches the head of each pull request on GitHub and maps it to github/pr/#. So then if someone sends a pull request and it is numbered 62 (for example), you would do:

git fetch github
git checkout -t github/pr/62

And then you would be on a local branch called pr/62 (assuming it didn't already exist). It's nice and means you don't have to keep track of other people's remotes or branches.

Ian Stapleton Cordasco
  • 26,944
  • 4
  • 67
  • 72
  • 2
    Why not? It explains exactly how to do this in a convenient and efficient way. – Ian Stapleton Cordasco Feb 19 '13 at 01:49
  • Because I am a noob, and that document is difficult to understand. I never would have gotten from "not getting it" to `git clone https://github.com/berstend/frappe.git -b feature/mongoose-support /my_clone` from the gist document. – Fresheyeball Feb 19 '13 at 03:27
  • 6
    What the gist document does is add an extra set of information (refs or references) to fetch from GitHub. When you do `git fetch github` you can then do `git co -t github/pr/#`. This prevents you from having to copy and paste the remote URL, figure out the branch name, etc. You then get well-named, concise and accurate branch names without the extra hassle. But I understand it may seem overwhelming. – Ian Stapleton Cordasco Feb 19 '13 at 03:31
  • Oh nice. I did not know this +1! Can you give me a fully qualified example? – Fresheyeball Feb 19 '13 at 06:41
  • @sigmavirus24 thanks a lot for the information; I wonder if similar trick exists for bitbucket? – Petr Kozelka Oct 31 '13 at 15:58
  • @PetrKozelka I'm not familiar with one for git but I know they do it automatically for mercurial. – Ian Stapleton Cordasco Nov 01 '13 at 01:19
7

When a user submits a pull request, they are asking for some changes to be merged from a branch on their clone of a fork back to another user's repository.

The changes you want can be got from the source of the pull request. To do this, clone the user's repository (git://github.com/berstend/frappe.git), and then check out the branch he created the pull request from (feature/mongoose-support).

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
5

To pull PR to your current branch use:

git pull origin pull/418/head

Here pull/{no. of PR}/head

enter image description here

Parth Developer
  • 1,371
  • 1
  • 13
  • 30
4

With Github's official new command line interface:

gh repo clone org/repo
cd repo
gh pr checkout 44

where 44 is the PR number, but can also be the branch name.


See additional details and options and installation instructions.

Xavier Guihot
  • 54,987
  • 21
  • 291
  • 190
3

For me, it was as simple as

git fetch origin pull/4/head

Where 4 was found here:

enter image description here

stevec
  • 41,291
  • 27
  • 223
  • 311
1

After installing git-extras

(cd /tmp && git clone --depth 1 https://github.com/tj/git-extras.git && cd git-extras && sudo make install)

You can simply use git pr

$ git pr 62 [remote]
megawac
  • 10,953
  • 5
  • 40
  • 61
1

BitBucket convention for listing and fetching PRs:

git ls-remote origin 'refs/pull-requests/*'
git fetch origin refs/pull-requests/998/from:local-branch-name

Full article here: https://www.atlassian.com/git/articles/pull-request-proficiency-fetching-abilities-unlocked

Vaiden
  • 15,728
  • 7
  • 61
  • 91
-1

That pull request shows the commits from that person's fork so you can see that he is pushing his changes from feature/mongoose-support branch.

You can clone his repository and checkout that branch

Kartik
  • 9,463
  • 9
  • 48
  • 52
-1

The accepted answer with "-b" option didn't work for me, so this is what I've ended up with. After pull request is created in Github, you'll be taken to the page where you can see something like below, where 'ogryb' is my user name and 'patch-1' is an automatically generated name assigned to it.

enter image description here

All you need to do now is to click on that highlighted name on the right, which will take you to a page with the following link:

enter image description here

Click on the right side of the green "Code" button to see a nice personalized git link that can be used for cloning or for adding more changes to the existing pull request.

Oleg Gryb
  • 5,122
  • 1
  • 28
  • 40
  • 1
    This won't be good for cloning someone else's pull request if they have since made other changes to their fork which the cloner does not want. – ijuneja May 31 '21 at 05:42