在Git中对称合并? [英] Are merges in Git symmetric?

查看:132
本文介绍了在Git中对称合并?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有两个分支( B C ),它们与共同的祖先 A 不同。从 B 合并到 C 会产生与从 C 合并到 B ?相同的结果吗?

  A 
|
/ \
BC

澄清 - 我假设任何手动合并冲突解决方案将在两个方向发生。但是会发生的自动合并导致选择相同的代码吗?这是我所假设的,因为提交日期在两个方向都是相同的。



为了进一步澄清 - 我知道实际合并结果是镜像彼此根据方向。我只是在问自动解决的冲突。

解决方案

默认合并的答案是肯定的。三向合并找到一个共同的祖先,然后应用来自双方的差异,这是一个不依赖于顺序的操作。合并排序和交换性的主题引起了关于 git list 的迷人讨论, (如果你进入那种事情,那就是)。注意 B到C C到B 应该是对称的,但对于(B到C)变成A B变成(C变成A)



根据下面的Vince的评论和seh对这个问题的评论,再详细一点,在 B到C 之间会有两个明显的区别, code>和 C转换为B ,这两者都不会影响问题中引用的自动合并解决方案。



首先,历史会有所不同。合并提交的父母将根据合并顺序进行更改。对于这些示例,我将使用first_branch和second_branch,所以我可以保留字母来表示提交。

  git checkout first_branch&& git merge second_branch 

E< - 合并提交
| \
| D< - second_branch的提示
| |
| C< - second_branch
|的另一个提交|
| B< - 和另一
| /
A< - first_branch在合并前的提示

在这种情况下,E的第一个父元素是 E ^ 1 ,它是first_branch在合并之前的提示。 second_branch是合并提交的第二个父,又名 E ^ 2 。现在考虑一下:

  git checkout second_branch&& git merge first_branch 

E< - merge commit
| \
| D< - first_branch的提示
| |
| C< - first_branch
|的另一个提交|
| B< - 和另一个
| /
A< - second_branch在合并前的提示

父母被逆转。 E ^ 1 是合并之前的second_branch的提示。 E ^ 2 是first_branch的提示。

其次,冲突的显示顺序将反转。在第一种情况下,冲突可能如下所示:

  <<<<<<<<<<<<<< HEAD 
这行是从first_branch分支添加的。
=======
这行是从second_branch分支添加的。
>>>>>>> second_branch

在第二种情况下,相同的冲突将如下所示:

 < <<<< < HEAD 
这行是从second_branch分支添加的。
=======
这行是从first_branch分支添加的。
>>>>>>> first_branch

这些差异都不会影响自动合并分辨率,但是当您将三路合并订单。


Lets say we have two branches (B and C) that have diverged from a common ancestor A. Will merging from B to C produce the same result as merging from C to B?

  A
  |
 / \
B   C

To clarify- I'm assuming that any manual merge conflict resolutions will occur in both directions. But will any automatic merges that occur result in the same code being chosen? This is what I'm assuming, since the commit dates are identical in both directions.

To further clarify - I know that the actual merging results in "mirror images" of each other based on the direction. I'm just asking about the automatically resolved conflicts.

解决方案

The answer is yes for default merges. A three-way merge finds a common ancestor and then applies the differences from both sides, an operation that isn't order dependent. The topic of merge-ordering and commutativity generated a fascinating discussion on the git list (if you're into that kind of thing, that is). Note B into C and C into B should be symmetric, but the same cannot necessarily be said for (B into C) into A versus B into (C into A).


To elaborate a bit more, based on Vince's comment below and seh's comment on the question, there will be two noticeable differences between B into C and C into B, neither of which affect the automatic merge resolution referenced in the question.

First, history will be different. The merge commit's parents will change depending on the merge order. For these examples, I'm going to use "first_branch" and "second_branch", so I can reserve letters to represent commits.

git checkout first_branch && git merge second_branch

E <- merge commit
|\
| D <- second_branch's tip
| |
| C <- another commit on second_branch 
| |
| B <- and another
|/
A <- first_branch's tip before the merge

In this case, the "first parent" of E, E^1, is first_branch's tip before the merge. second_branch is the "second parent" of the merge commit, aka E^2. Now consider the reverse:

git checkout second_branch && git merge first_branch

E <- merge commit
|\
| D <- first_branch's tip
| |
| C <- another commit on first_branch 
| |
| B <- and another
|/
A <- second_branch's tip before the merge

The parents are reversed. E^1 is the tip of second_branch before the merge. E^2 is the tip of first_branch.

Second, the display order of conflicts will reverse. In the first case, a conflict might look like this:

<<<<<<< HEAD
This line was added from the first_branch branch.
=======
This line was added from the second_branch branch.
>>>>>>> second_branch

In the second case, the same conflict would look like this:

<<<<<<< HEAD
This line was added from the second_branch branch.
=======
This line was added from the first_branch branch.
>>>>>>> first_branch

Neither of these differences affect automatic merge resolution, but they do appear when you reverse three-way merge order.

这篇关于在Git中对称合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆