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

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

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?

回答1:

To fetch a pull into your repository:

git fetch git@github.com:jboss/jboss-common-beans.git refs/pull/4/head 

Then do whatever you want with FETCH_HEAD:

git checkout -b new-branch FETCH_HEAD 


回答2:

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?



回答3:

You can do this:

1) Add the upstream remote:

git remote add upstream git@github.com: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 


回答4:

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



回答5:

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 


回答6:

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 - 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 "-"]     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 

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



回答7:

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

$ git remote add upstream git@github.com: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' 


回答8:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!