git 如何为我不拥有的分支获取未合并的拉取请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6743514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I fetch an unmerged pull request for a branch I don't own?
提问by wolfc
I need to pull in a specific pull request (that hasn't been processed into the main stream yet) in the NServiceBus repo:
我需要在 NServiceBus 存储库中拉入特定的拉取请求(尚未处理到主流中):
https://github.com/johnsimons/NServiceBus/commit/d8524d53094e8181716e771c1023e968132abc15
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?
做这个的最好方式是什么?
回答by wolfc
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:
然后用 FETCH_HEAD 做任何你想做的事:
git checkout -b new-branch FETCH_HEAD
回答by Steven Penny
git pull origin pull/28/head
Or
或者
git fetch origin pull/28/head:28
git checkout 28
回答by Elias Dorneles
You can do this:
你可以这样做:
1) Add the upstream remote:
1)添加上游遥控器:
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:
2) 之后,您可以根据其 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.
然后你会有一个NEW_BRANCH_NAME
包含 PR 代码的分支。
Adding an alias:
添加别名:
If you do this as often as me, you may want to setup some aliasesfor it. I have this in my .gitconfig:
如果你像我一样经常这样做,你可能想为它设置一些别名。我的 .gitconfig 中有这个:
[alias]
fetch-pr = "!f(){\
[ -z \"\" ] && { echo Usage: git fetch-pr PULL_REQUEST_ID [REMOTE_NAME] [NEW_BRANCH_NAME]; exit 1; }; \
remote=${2:-origin}; \
branch=${3:-pr-}; \
git fetch $remote \"pull//head:$branch\"; \
}; f "
pr = "!f(){\
branch=${3:-pr-}; \
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
回答by cat
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.
对于困难的情况(特别是如果您还没有签出 git-repo),我认为最简单的方法是应用补丁。为此,只需在 github 上打开 pull-request 并向 URL 添加“.patch”,下载它并应用补丁。
Example:
例子:
cd cordova-plugin-media
wget https://github.com/apache/cordova-plugin-media/pull/120.patch
patch -p1 < 120.patch
回答by David Phillips
See this help article from GitHub: https://help.github.com/articles/checking-out-pull-requests-locally
请参阅 GitHub 上的这篇帮助文章:https: //help.github.com/articles/checking-out-pull-requests-locally
回答by Tomas Tomecek
Once you added the upstream repo as an upstream remote (as @elias pointed out):
一旦您将上游存储库添加为上游远程(如@elias 所指出的):
$ git remote add upstream [email protected]:Particular/NServiceBus
You can configure git to fetch pull requests by default:
您可以配置 git 默认获取拉取请求:
$ 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'
回答by kmario23
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 mergedinto the master or any other branch yet. So,
我假设一个人已经在本地将一个 repo(例如pytorch)克隆到他/她的系统中。之后,一些志愿者/爱好者贡献了一些代码并向远程存储库发布了 PR,但尚未合并到 master 或任何其他分支。所以,
First we'll have to do git remote add
to the github remote repository:
首先,我们必须git remote add
对 github 远程存储库执行以下操作:
# 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:
然后cd
进入存储库pytorch
,然后只需执行以下操作:
# 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 fetchedinto 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:
现在,待处理的 PR 已被提取到您的本地存储库中,并且您的提取提示将在 FETCH_HEAD 中。如果你想在本地合并这个待处理的 PR ,那么只需执行以下操作:
$ 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.
您应该能够看到本地存储库n
在作为待处理 PR 一部分的提交之前(即,可以在单个 PR 中发出 1 个以上的提交)。因此,提交的数量取决于待处理 PR 中包含的提交。
回答by BobHy
Github has clear doc for merging the pull request into a localrepo:
Github 有明确的文档用于将拉取请求合并到本地存储库中:
https://help.github.com/en/articles/checking-out-pull-requests-locally
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.
归结为知道 GitHub 中的拉取请求只是基本存储库中的一个分支,其中分支名称是序列号。上面的文章向您展示了如何找到这个数字和 git 命令行魔法,以使用您喜欢的任何分支名称将其拉入您的本地存储库。
I could not find a similarly simple way to merge the pull request into a fork I created on GitHub.
我找不到类似的简单方法将拉取请求合并到我在 GitHub 上创建的分叉中。
回答by Waaheeda
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!!!!!!!!!!!!!!
我找到了这个问题的解决方案 - 从不同机器上未合并的 PR 中提取更改,我在 git bash 1 上做了以下事情。您必须在机器 2 上克隆远程存储库 2. 执行 git checkout(生成 PR 的分支) 3. 执行 git pull 并完成!!!!!!!!!!!!!!
回答by not2qubit
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.
相反,只需进入您自己的存储库并使用其提交哈希拉入提交(来自 PR 源)。
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 committhese with some tag/label.
这样做,您将只拥有一堆新编辑过的文件,就好像您自己编辑过它们一样。如果您想使用一些标签/标签提交这些,则由您决定。
If you then want to push all news up to your own GitHub repo, just do as always:
如果您想将所有新闻推送到您自己的 GitHub 存储库,请照常执行:
git commit -m "Added something by Anonymous"
git push -u origin master