3-Way Merge in git - how comparing works

梦想的初衷 提交于 2020-01-06 05:17:25

问题


I have three versions of the file:

version 1       common ancestor      version  2
-------------   ---------------      ------------- 
before          original line        original line
original line                        after

What happens while comparing these versions to produce final merge version ?

I have read some information about this topic, but I am still confused how exactly it works.

As for latter example:

Is comparing individual lines between versions linear ? If so, then final merge should look like this:

1 line: before
2 line: conflict (both left and right contributors are changed compared to ancestor) 

Is this correct understanding or does it work differently ?


回答1:


A three-way merge usually means that instead of just comparing the final result to perform the merge, the common base version is already being looked at. What Git then does is create a representation of changes for each version.

So what it actually gets, relative to the base version, is the following:

version 1           version  2
-------------       -------------
+before              original line
 original line      +after

It will then use common lines as context to align the changes:

version 1           version  2
-------------       -------------
+before
 original line       original line
                    +after

At which point, the merge is easy to resolve without a conflict to the following:

before
original line
after

Note that such a merge may still result in a conflict because Git might not have enough common content to align the changes properly. Especially for very small files this might happen.




回答2:


I don't think merge is done by "comparing" individual lines straight between version 1 and version 2. It's more involved than that. It's about trying to see where the diffs between common-ancestor..version1 and common-ancestror..version2 can be "merged". In your particular case, there's a single line on the original version, right? I think that if the line had an EOL at the end before the EOF, then merging them won't break (because that line would be present on both final versions) and so it would be merged perfectly fine. However, if the line did not have an EOL at the end, then version 2 would have removed that line (because the original line is not there anymore.... now it's a different line because of the EOL) and then you would end up with a conflict.



来源:https://stackoverflow.com/questions/44724007/3-way-merge-in-git-how-comparing-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!