git git合并冲突究竟是如何发生的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24852116/
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
how does exactly a git merge conflict happen?
提问by user3693167
I have made a git repository and added a text file to it. This is 100% for learning purpose.
我已经创建了一个 git 存储库并向其中添加了一个文本文件。这是 100% 用于学习目的。
I added "1" to the text file and committed it to master.
Created a new branch from master and appended "2".
Finally, created a branch from master and appended "3".
我在文本文件中添加了“1”并将其提交给 master。
从 master 创建了一个新分支并附加了“2”。
最后,从 master 创建了一个分支并附加了“3”。
Could you please explain how a conflict may occur in this, or any other, scenario?
您能否解释一下在这种情况下或任何其他情况下如何发生冲突?
采纳答案by VonC
You will have a conflict if you merge:
如果合并,则会发生冲突:
branch2
tomaster
(no conflict)branch3
tomaster
(conflict):
branch2
到master
(无冲突)branch3
到master
(冲突):
That is because:
那是因为:
- The common ancestor would be
master
(with a second line empty) - the source content is
branch3
(with a second line including "3") - the destination content is on latest of
master
(with a second line including "2", from the merge ofbranch2
tomaster
)
- 共同的祖先将是
master
(第二行为空) - 源内容是
branch3
(第二行包括“3”) - 目标内容是最新的
master
(第二行包括“2”,从合并branch2
到master
)
Git will ask you to choose which content to keep ("3", "2", or both).
Git 会要求您选择要保留的内容(“3”、“2”或两者)。
First, do the merges after:
首先,在以下之后进行合并:
git config merge.conflictstyle diff3
See "Fix merge conflicts in Git?".
请参阅“修复 Git 中的合并冲突?”。
回答by Caleb
A merge conflict happens when two branches both modify the same region of a file and are subsequently merged. Git can't know which of the changes to keep, and thus needs human intervention to resolve the conflict.
当两个分支都修改文件的同一区域并随后合并时,就会发生合并冲突。Git 不知道要保留哪些更改,因此需要人工干预来解决冲突。
In this case, your steps 2 and 3 create two branches that have conflicting changes.
在这种情况下,您的第 2 步和第 3 步会创建两个具有冲突更改的分支。
回答by faisal_kk
I understand this is an old question but in case you would like to know in an intuitive way the algorithms used by Git to compare two files.It will clarify the doubts on how overlapping regions works with diff.
我知道这是一个老问题,但如果您想以直观的方式了解 Git 用于比较两个文件的算法。它将澄清关于重叠区域如何与 diff 一起工作的疑问。
Here is an explanation of one of the popular algorithms which was developed by Eugene W. Myers. In this approach finding the shortest edit script (SES) is modelled as a graph search. Here is really good article by James Coglan on the same with an example- The Myers diff algorithm
这是对 Eugene W. Myers 开发的一种流行算法的解释。在这种方法中,寻找最短的编辑脚本 (SES) 被建模为图形搜索。这是James Coglan 的一篇非常好的文章,并附有示例- Myers diff 算法