GitHub clone from pull request?

GitGithubPull Request

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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

Solution 3 - Git

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

Solution 4 - Git

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 = [email protected]: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.

Solution 5 - Git

git clone git://github.com/dweldon/frappe
cd frappe
git pull origin pull/2/head

https://stackoverflow.com/q/6743514/#32443551

Solution 6 - Git

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

Solution 7 - Git

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.

Solution 8 - Git

For me, it was as simple as

git fetch origin pull/4/head

Where 4 was found here:

enter image description here

Solution 9 - Git

To pull PR to your current branch use:

git pull origin pull/418/head

Here pull/{no. of PR}/head

enter image description here

Solution 10 - Git

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]

Solution 11 - Git

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

Solution 12 - Git

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

Solution 13 - Git

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.

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
QuestionFresheyeballView Question on Stackoverflow
Solution 1 - GitChronialView Answer on Stackoverflow
Solution 2 - GitinancsevincView Answer on Stackoverflow
Solution 3 - GitwierzbiksView Answer on Stackoverflow
Solution 4 - GitIan Stapleton CordascoView Answer on Stackoverflow
Solution 5 - GitZomboView Answer on Stackoverflow
Solution 6 - GitadrianbanksView Answer on Stackoverflow
Solution 7 - GitXavier GuihotView Answer on Stackoverflow
Solution 8 - GitstevecView Answer on Stackoverflow
Solution 9 - GitParth DeveloperView Answer on Stackoverflow
Solution 10 - GitmegawacView Answer on Stackoverflow
Solution 11 - GitVaidenView Answer on Stackoverflow
Solution 12 - GitKartikView Answer on Stackoverflow
Solution 13 - GitOleg GrybView Answer on Stackoverflow