为什么 3 路合并比 2 路合并更有优势?
维基百科说三向合并比双向合并,通常不需要用户干预。为什么会这样呢?
3 路合并成功而 2 路合并失败的示例会很有帮助。
Wikipedia says a 3-way merge is less error-prone than a 2-way merge, and often times doesn't need user intervention. Why is this the case?
An example where a 3-way merge succeeds and a 2-way merge fails would be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您和您的朋友都签出了一个文件,并对它进行了一些更改。您在开头删除了一行,您的朋友在末尾添加了一行。然后他提交了他的文件,您需要将他的更改合并到您的副本中。
如果您正在进行双向合并(换句话说,差异),该工具可以比较两个文件,并看到第一行和最后一行不同。但它怎么知道如何处理这些差异呢?合并版本应该包含第一行吗?它应该包括最后一行吗?
通过三向合并,它可以比较两个文件,但也可以将每个文件与原始副本进行比较(在您更改文件之前)。因此它可以看到您删除了第一行,并且您的朋友添加了最后一行。它可以使用该信息来生成合并版本。
Say you and your friend both checked out a file, and made some changes to it. You removed a line at the beginning, and your friend added a line at the end. Then he committed his file, and you need to merge his changes into your copy.
If you were doing a two-way merge (in other words, a diff), the tool could compare the two files, and see that the first and last lines are different. But how would it know what to do with the differences? Should the merged version include the first line? Should it include the last line?
With a three-way merge, it can compare the two files, but it can also compare each of them against the original copy (before either of you changed it). So it can see that you removed the first line, and that your friend added the last line. And it can use that information to produce the merged version.
这张来自 perforce 演示文稿的幻灯片很有趣:
This slide from a perforce presentation is interesting:
三向合并是指将两个变更集在应用时合并到一个基本文件,而不是应用一个变更集,然后将结果与另一个合并。
例如,在同一位置添加一行的两次更改可能会被解释为两次添加,而不是一行的更改。
例如,文件
a
已被两个人修改,其中一个添加了moose
,另一个添加了mouse
。现在,如果我们在应用变更集时合并它们,我们将得到(三向合并)
但是如果我们应用 b,然后查看从 b 到 c 的更改,看起来我们只是将“u”更改为'o'(2 路合并)
A three-way merge is where two changesets to one base file are merged as they are applied, as opposed to applying one, then merging the result with the other.
For example, having two changes where a line is added in the same place could be interpreted as two additions, not a change of one line.
For example, file
a
has been modified by two people, one addingmoose
, one addingmouse
.Now, if we merge the changesets as we apply them, we will get (3-way merge)
But if we apply b, then look at the change from b to c it will look like we are just changing a 'u' to an 'o' (2-way merge)
在日常工作中,只要有可能,我们都会做或者更愿意让 git 做
双向合并
。问题是为什么需要3 路合并
,而不是默认的2 路合并
。默认的合并策略是
快进合并
(见下左图),但是如果git
无法进行快进合并
因为开发历史已经出现分歧,示例额外提交已提交到源分支(但不是由您提交),git
必须做一些工作,并且它使用三向合并< /code>(下右图)。可以在此处找到推荐阅读内容。当然,对于
git
的所有内容,您还可以通过使用选项--no-ff
来决定进行3 向合并
,即使不需要。借自 AWS CodeCommit:
开发者工具>代码提交>存储库>存储库名称>
拉取请求>拉取请求名称>合并
In day-to-day work, we would do or rather let git do a
2-way merge
whenever possible. The question would be why is a3-way merge
needed, instead of the default2-way merge
.The default merge strategy is a
fast-forward merge
(see left picture below), but ifgit
can't do afast-forward merge
because the development history has diverged, example extra-commits has been committed to the source branch(but not by you),git
has to do some work and it uses athree-way merge
(right picture below). A recommended read can be found here. Of course, with all thingsgit
, you can also decide to do a3-way merge
by using the option--no-ff
, even when it is not required.Borrowed from AWS CodeCommit:
Developer Tools > CodeCommit > Repositories > RepositoryName >
Pull requests > Pull request name > Merge