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

13

92

I need to pull in a specific pull request (that hasn't been processed into the main stream yet) in the NServiceBus repo:

https://github.com/johnsimons/NServiceBus/commit/d8524d53094e8181716e771c1023e968132abc15

It's obviously not my repo, but I need the changes that exist in that pull request.

What is the best way to do this?

Antifebrile answered 19/7, 2011 at 7:16 Comment(2)
possible duplicate of Pull requests from other forks into my forkKee
OP said that they needed to grab the PR, which means downloading the content. It's not exactly clear whether they want to just view the content or incorporate it into their repo. Meanwhile in the linked question, the asker specifically said that they wanted to apply the PR into their fork. So the two questions are technically not duplicates.Upcast
I
93
git pull origin pull/28/head

Or

git fetch origin pull/28/head:28
git checkout 28

Can I pull a not-yet-merged pull request?

Ingather answered 19/7, 2011 at 7:16 Comment(2)
I think it's worth noting that git pull will keep you on your current branch and merge in the PR changes while git fetch/checkout will simply switch you to the PR branch.Whitley
You don’t need refs before pull/28/head?Orff
M
77

To fetch a pull into your repository:

git fetch [email protected]:jboss/jboss-common-beans.git refs/pull/4/head

Then do whatever you want with FETCH_HEAD:

git checkout -b new-branch FETCH_HEAD
Mammon answered 28/3, 2012 at 7:48 Comment(5)
Trying this, I get an error: fatal: Couldn't find remote ref refs/pull/1041/head. =/Incise
You will want to set [email protected]:jboss/jboss-common-beans.git as a remote upstream, and then do: git fetch upstream refs/pull/4/headBorsch
And how do you then push to PR?Reinert
You can't push to pull/NN/head refs, only to the branch the PR is tracking (see "user wants to merge ... from user:branch-name" under PR title). If it's not your PR you frequently can't push, but there are several ways author can give permission help.github.com/articles/…Istria
What if the user updates the PR, and I want to pull the update?Sheryllshetland
I
29

You can do this:

1) Add the upstream remote:

git remote add upstream [email protected]:Particular/NServiceBus.git

2) After that, you can checkout any pull request to a new branch per its ID:

git fetch upstream pull/PULL_REQUEST_ID/head:NEW_BRANCH_NAME

Then you'll have a branch named NEW_BRANCH_NAME containing the PR code.

Adding an alias:

If you do this as often as me, you may want to setup some aliases for it. I have this in my .gitconfig:

[alias]
    fetch-pr = "!f(){\
        [ -z \"$1\" ] && { echo Usage: git fetch-pr PULL_REQUEST_ID [REMOTE_NAME] [NEW_BRANCH_NAME]; exit 1; }; \
        remote=${2:-origin}; \
        branch=${3:-pr-$1}; \
        git fetch $remote \"pull/$1/head:$branch\"; \
        }; f "
    pr = "!f(){\
        branch=${3:-pr-$1}; \
        git fetch-pr \"$@\"; \
        git switch $branch; \
        }; f "

With the above, I can do:

git fetch-pr 123              # fetch PR #123 into branch pr-123
git fetch-pr 123 some-branch  # fetch PR #123 into some-branch
git pr 123                    # fetch and switch to the branch
Incise answered 10/3, 2015 at 0:34 Comment(1)
In case anyone forget, there is a 3rd step: git checkout NEW_BRANCH_NAMETramel
H
9

For difficult situations (especially if you have not a checked out git-repo), I think the simplest way is to apply a patch. For this just open the pull-request on github and add a ".patch" to the URL, download it and apply the patch.

Example:

cd cordova-plugin-media
wget https://github.com/apache/cordova-plugin-media/pull/120.patch
patch -p1 < 120.patch
Hyrup answered 17/11, 2016 at 11:56 Comment(0)
S
5

See this help article from GitHub: https://help.github.com/articles/checking-out-pull-requests-locally

Satanism answered 21/5, 2014 at 23:44 Comment(0)
B
4

github/hub

https://github.com/github/hub is a GitHub CLI helper that deals with this and other use cases beautifully using extra information from the GitHub API. E.g.:

git clone https://github.com/github/hub
# Just copy paste the URL.
hub checkout https://github.com/github/hub/pull/970

Result:

  • we are now on a branch called <USERID>-<BRANCH_NAME> that contains the PR.

    Note the good branch name which was automatically set for us.

  • that branch is set to track the original branch on the fork, i.e. .git/config contains:

    [branch "<USERID>-<BRANCH_NAME>"]
        remote = retronym
        merge = refs/heads/ticket/969
        rebase = true
    

    So if further commits get pushed, we can git fetch them directly.

Installing hub on Linux is currently a pain if you're not familiar with Go, but worth it. On Ubuntu 14.04, the Go on the repositories is too old, so GVM is the best option:

bash < <(curl -LSs 'https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer')
. "$HOME/.gvm/scripts/gvm"
gvm install 'go1.4'
gvm use 'go1.4' --default
go get github.com/github/hub

I have also asked GitHub to give us a copy paste cheatsheet on the web UI at: https://github.com/isaacs/github/issues/449

Borsch answered 17/8, 2015 at 10:33 Comment(4)
Do you have any source on the refs/heads/ticket/NN? I've never seen this magical ref, only pull/NN/head (this is what hub currently gave me) & pull/NN/merge. just curious...Istria
@BeniCherniavsky-Paskin hi Beni, I couldn't find any docs on it. I guess it's the perks of using APIs your own enterprise created.Borsch
it might be beneficial to change the demo of using hub to use a different github project - any other but hub, otherwise it can be confusing that you're working with hub on hub.Firestone
it looks that perhaps hub has changed its functionality, at least I don't get "<USERID>-<BRANCH_NAME>", but the same branch name as the PR's branch name, w/o userid.Firestone
S
3

Once you added the upstream repo as an upstream remote (as @elias pointed out):

$ git remote add upstream [email protected]:Particular/NServiceBus

You can configure git to fetch pull requests by default:

$ git config --local --add remote.upstream.fetch '+refs/pull/*/head:refs/remotes/upstream/pr/*'

So, let's fetch it:

$ git fetch upstream
Fetching upstream
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/Particular/NServiceBus
 * [new ref]         refs/pull/1/head -> upstream/pr/1
 * [new ref]         refs/pull/2/head -> upstream/pr/2

And check it out:

$ git checkout pr/2
Branch pr/2 set up to track remote branch pr/2 from upstream.
Switched to a new branch 'pr/2'
Superintendency answered 4/7, 2017 at 7:2 Comment(0)
N
3

Here're the commands that worked for me.

I'll assume that one has already cloned a repo (for e.g. pytorch ) to his/her system locally. After that some volunteer/enthusiast has contributed some code and issued a PR to the remote repository but it has not been merged into the master or any other branch yet. So,

First we'll have to do git remote add to the github remote repository:

# I've given the name `original`; you can give some other name as per your liking
$ git remote add original https://github.com/pytorch/pytorch

Then cd into the repository pytorch and then simply do:

# after this, the unmerged PR should be pulled to your local repo
$ git fetch original pull/<pull_number>/head    # 23, 123 etc.,

Now, the pending PR has been fetched into your local repo and the tip of your fetch would be in FETCH_HEAD. If you want to merge this pending PR locally, then simply do:

$ git merge FETCH_HEAD

After this, if you do:

$ git status

You should be able to see that the local repo is ahead of n commits that were part of the pending PR (i.e. it's possible to issue more than 1 commit in a single PR). So, the number of commits depend on the commits contained in the pending PR.

Nablus answered 11/4, 2019 at 13:8 Comment(0)
S
2

This is the solution from GitHub docs:

From your project repository, check out a new branch and test the changes.

git checkout -b GithubUserID-branchName branchName
git pull https://github.com/GithubUserID/reponame.git branchName

Where:

  1. GithubUserID is the username of the person who opened the pull request.
  2. branchName is the name of the branch for example master
  3. reponame the repository name like demo-todo

Example :

git checkout -b felix123-master master
git pull https://github.com/felix123/demo-todo.git master
Specialism answered 26/6, 2020 at 16:22 Comment(0)
C
1

If you just want to add one unmerged Pull Request from some other repo, to your own, there is no need for all complications (as mostly shown in other answers).

Instead just go into your own repo and pull in the commit (from PR source), using its commit hash.

git pull https://bitbucket.org/SomeUser/SomeProjectRepo/commits/c15...db2

Doing it this way, you will just have a bunch of new edited files, as if you had edited them yourself. It's then up to you if you want to commit these with some tag/label.

If you then want to push all news up to your own GitHub repo, just do as always:

git commit -m "Added something by Anonymous"
git push -u origin master
Complacence answered 2/11, 2018 at 21:30 Comment(0)
A
0

below is to make your 'git fetch' command to fetch all pull requests when run "git fetch"

add below into ~/.gitconfig

[remote "origin"]
fetch = +refs/pull-requests/*/from:refs/remotes/origin/pr/*

note the reference "refs/pull-requests/" has the stash naming convention, for git hub, you may need different format

Accipitrine answered 28/3, 2017 at 10:34 Comment(0)
O
0

Github has clear doc for merging the pull request into a local repo:

https://help.github.com/en/articles/checking-out-pull-requests-locally

It boils down to knowing that a pull request in GitHub is just a branch in the base repo where the branch name is a sequence number. The above article shows you how to find this number and the git command line magic to pull it into your local repo with whatever branch name you'd like.

I could not find a similarly simple way to merge the pull request into a fork I created on GitHub.

Olympian answered 20/4, 2019 at 18:40 Comment(1)
The header of this article says that is only works for people who have write access to the repo. OP says they don't have write access.Exceeding
P
0

I found this solution to this problem - pulling changes from unmerged PR on different Machine , I did following things on git bash 1. You must have taken clone of remote repository on machine 2 2. do git checkout (branch on which PR was generated) 3. do git pull and done!!!!!!!!!!!!!!

Per answered 29/8, 2019 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.