git 为 github 上的项目做出贡献,如何“将我的拉取请求重新设置在 master 之上”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17182624/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:24:02  来源:igfitidea点击:

Contributing to project on github, how to "rebase my pull request on top of master"

gitgithub

提问by fontno

Ok so I an contributing to a project on github. The project on github is upstream, my forked repo on github is origin, and my localrepo on my computer.

好的,所以我在 github 上为一个项目做贡献。github 上的项目是upstream,我在 github 上的分叉 repo 是origin,我local在我的电脑上的 repo 。

git checkout -b feature
# Working on feature
git commit -a -m 'only commit on feature'

then I submit a pull request

然后我提交拉取请求

git push origin master

The pull request is reviewed and a unrelated change needs to be made. Someone else makes a commit and merge into upstream/master

拉取请求并需要进行不相关的更改。其他人提交并合并到upstream/master

Now I am asked by the upstreammaintainer to "rebase my pull request on top of master"

现在upstream维护者要求我“在 master 上重新设置我的 pull request”

This is my story (insert Law and Order sound effect).....

这是我的故事(插入法律与秩序音效).....

I did not make any changes to the pull request and its still the same commit on branch feature.

我没有对拉取请求进行任何更改,它仍然是对分支功能的相同提交。

git checkout master
git fetch upstream
git checkout feature
git rebase master
=> "Current branch feature is up to date."
git push origin feature
=> "Everything up-to-date"

I don't understand. How is this possible when I know that someone committed and merged to upstream/masterafter I pushed my pull request to origin/feature?

我不明白。当我知道upstream/master在我将拉取请求推送到之后有人提交并合并时,这怎么可能origin/feature

Can anyone tell me what the correct procedure should be in this situation?

谁能告诉我在这种情况下正确的程序应该是什么?

回答by Ryan Stewart

You only show a fetch on the upstream repo. That doesn't actually update any of your local branches. It only updates your knowledge of upstream. You'd need to ensure upstream/masteris fully merged into your master, like with a git pull, prior to rebasing onto master, or more simply just rebase onto upstream/master.

您只在上游存储库上显示提取。这实际上并没有更新您的任何本地分支机构。它只会更新您对upstream. 你需要确保upstream/master完全合并到你master喜欢用,git pull之前衍合入到master,以上只是简单地重订到upstream/master

I.e:

IE:

git checkout master
git pull upstream master
git checkout feature
git rebase master

or

或者

git checkout feature
git rebase upstream/master

Update:

更新:

After fixing your local featurebranch, you'll need to push it back to originto finish updating the pull request. Since you've pushed featureonce already, you can't simply pushagain because a rebase changes history, and it's no long a fast-forward. Normally, whan a push fails with a "non-fast-forward", you'd resolve it by doing a pull, but a pull will just combine the two divergent histories, which is definitely not what you want. That would mean your old (pre rebase) featurebranch would be combined with the new (post rebase) one. You want to overwriteorigin/featurewith the state of the new featurebranch, dumping any record of the old one. That means you'll want to force the push to happen, even though it's not a fast-forward, using git push -f origin feature. Note: force pushing is dangerous, and you can lose commits with it. Only use it if you're absolutely sure you know what you're doing, like right here, where you intentionally want to drop the old, useless commits in the pre-rebase featurebranch.

修复本地feature分支后,您需要将其推回origin以完成拉取请求的更新。既然你已经推送feature过一次,你不能简单地push再次推送,因为 rebase 改变了历史,而且它不再是快进了。通常,如果推送因“非快进”而失败,您可以通过拉动来解决它,但拉动只会结合两个不同的历史,这绝对不是您想要的。这意味着您的旧(rebase 前)feature分支将与新(rebase 后)分支合并。你想用新分支的状态覆盖,转储旧分支的任何记录。这意味着您会想要强制推送发生,即使它不是快进,使用. 笔记:origin/featurefeaturegit push -f origin feature危险,你可能会失去对它的提交。仅当您绝对确定自己知道自己在做什么时才使用它,就像在这里,您有意删除 pre-rebasefeature分支中旧的、无用的提交。

回答by VonC

Now I am asked by the upstream maintainer to "rebase my pull request on top of master"

现在上游维护者要求我“在 master 上重新设置我的 pull request”

Note that since Sept. 2016, the maintainer can trigger the rebase himself/herself.

请注意,自 2016 年 9 月起,维护者可以自己触发 rebase。

See "Rebase and merge pull requests"

请参阅“变基和合并拉取请求

When you select the new "Rebase and merge" option, the commits from the pull request's branch are rebased on to the tip of the base branch, and then the base branch itself is fast forwarded to this newly rebased head. Rebases automatically set the committer of the rebased commits to the current user, while keeping authorship information intact. The pull request's branch will not be modified by this operation.

If a rebase can't be performed due to conflicts, we'll let you know so you can manually resolve them as necessary.

当您选择新的“Rebase and merge”选项时,来自拉取请求分支的提交将重新基于基础分支的尖端,然后基础分支本身会快速转发到这个新的重新定位的头部。变基自动将变基提交的提交者设置为当前用户,同时保持作者信息完整。此操作不会修改拉取请求的分支。

如果由于冲突而无法执行变基,我们会通知您,以便您可以根据需要手动解决它们。

https://cloud.githubusercontent.com/assets/2195/18671961/a03fa9b6-7f35-11e6-8fa0-e16b2fede8ca.gif

https://cloud.githubusercontent.com/assets/2195/18671961/a03fa9b6-7f35-11e6-8fa0-e16b2fede8ca.gif