git 樱桃选择合并

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

Cherry-pick a merge

gitversion-control

提问by Readonly

Let's say branch B is a topic branch off of branch A, and you want those changes in branch C. What does it mean when you cherry-pick the merge commit of branch A and branch B to branch C?

假设分支 B 是分支 A 的主题分支,并且您希望分支 C 中的这些更改。当您挑选分支 A 和分支 B 的合并提交到分支 C 时,这意味着什么?

For example, if you use the -m flag to specify the old HEAD of branch A to cherry-pick the merge to branch C, does that simply mean "Take the diff between the cherry-picked commit tree and old HEAD of branch A and apply it to branch C?"

例如,如果您使用 -m 标志来指定分支 A 的旧 HEAD 来挑选合并到分支 C,这是否仅意味着“在挑选出的提交树和分支 A 的旧 HEAD 和将它应用到分支 C?”

Are there any gotchas for using this method? (e.g. Would branch C look like it's merged to branch A and B? Would more changes be applied than simply the commits from branch B?)

使用这种方法有什么问题吗?(例如,分支 C 会不会看起来像是合并到分支 A 和 B?会应用更多的更改而不是简单地来自分支 B 的提交?)

采纳答案by Greg Hewgill

The way I usually do this is using git rebase:

我通常这样做的方式是使用git rebase

git rebase --onto C A B

This takes the diffs between A and B, and applies those diffs to branch C. As a bonus, the rebase will skip any commits between A and B that perform the same textual change as already exists in branch C.

这需要 A 和 B 之间的差异,并将这些差异应用于分支 C。作为奖励,rebase 将跳过 A 和 B 之间执行与分支 C 中已存在的文本更改相同的文本更改的任何提交。

Update: In the case you mentioned in the comments, remember that Git never overwrites past history. So even after doing the rebase above, you could recreate a new branch head at the commit where B used to be before the rebase. Unfortunately I can't think of a simple way to do that at this time of the morning. Sorry I couldn't be more help, perhaps somebody else will come up with an easy way!

更新:对于您在评论中提到的情况,请记住 Git 永远不会覆盖过去的历史记录。因此,即使在执行上面的 rebase 之后,您也可以在提交时重新创建一个新的分支头,其中 B 曾经在 rebase 之前所在的位置。不幸的是,在早上的这个时候,我想不出一个简单的方法来做到这一点。对不起,我帮不上忙了,也许其他人会想出一个简单的方法!