简体   繁体   English

为什么git在合并期间会产生冲突,而大多数kbin3之类的合并工具会自动解决该冲突?

[英]Why does git generate conflicts during a merge that are auto-resolved by most merge tools such as kdiff3?

As stated in the title. 如标题中所述。 I tried googling this but found no explanation for it so I looked at the kdiff3 documentation which provided some insight. 我尝试使用谷歌搜索,但没有找到解释,所以我看了kdiff3文档,该文档提供了一些见识。 The situation i'm running into is I do a merge from one branch into another, get a bunch of conflicts and use git mergetool to resolve them one by one. 我遇到的情况是我从一个分支合并到另一个分支,遇到一堆冲突,然后使用git mergetool逐个解决它们。 What happens is that for some files when kdiff3 is started, it pops up a dialog that there were x auto resolved conflicts and that there are no conflicts left so i save the file and continue. 发生的是,对于某些文件,当启动kdiff3时,它会弹出一个对话框,提示存在x个自动解决的冲突,没有冲突,因此我保存文件并继续。

According to kdiff3 documentation, conflicts are auto resolved if the line was changed in only one version of the file. 根据kdiff3文档,如果仅在文件的一个版本中更改了该行,则冲突会自动解决。 Ie. IE浏览器。 the base is the same as the local or remote file, but not both. 基本名称与本地或远程文件相同,但不能两者都相同。

My question is, if my merge tool can auto-resolve this(i've tried a few and they all auto resolve some conflicts) and indeed it seems logical what it does, why do I even get a conflict from git? 我的问题是,如果我的合并工具可以自动解决此问题(我已经尝试了几次,并且它们都可以自动解决一些冲突),并且确实这样做是合乎逻辑的,为什么我什至会从git中得到冲突?

A follow up question is: how can I make git auto-resolve these conflicts before i even pop open my merge tool? 后续问题是:在弹出合并工具之前,如何使git自动解决这些冲突?

An example would be: 一个例子是:

base:
line 1
line 2
line 3

local:
line 1
line 2
line 3

remote:
line 1
line abc
line 3

I think the above conflicts in Git but is auto resolved in kdiff3. 我认为以上冲突在Git中,但在kdiff3中已自动解决。 Though it might be a different case, i've done merges that were auto-resolved before, can't remember the exact scenario since it auto-resolved. 尽管情况可能有所不同,但我已经完成了以前自动解决的合并,因为自动解决了,所以不记得确切的情况。

There could be a number of reasons, but most likely due to a difference in merge preferences. 可能有多种原因,但最有可能是由于合并首选项的差异。

Some examples of differences which kdiff3 can be configured to ignore: 可以配置kdiff3忽略的一些差异示例:

  • white space: an empty line and a line with four spaces 空白:一个空行和一个有四个空格的行
  • comments: /* This is the original comment */ and /* This is the modified comment */ 评论: /* This is the original comment *//* This is the modified comment */
  • numbers: number_of_diffs = 3 and number_of_diffs = 7 数字: number_of_diffs = 3number_of_diffs = 7

In each of those cases (and potentially many more) kdiff3 may just ignore the differences, thereby having no problem merging. 在每种情况下(甚至可能更多),kdiff3都可以忽略差异,因此合并时没有问题。 Git, on the other hand, will make you manually resolve the conflicts. 另一方面,Git将使您手动解决冲突。

In your example, there is no obvious reason why any (reasonable) tool would issue a merge-conflict. 在您的示例中,没有任何明显的理由说明任何(合理的)工具都会发出合并冲突。 But what if the base file had been created in Linux, whilst you had edited the local file in Windows (or vice versa)? 但是,如果基本文件是在Linux中创建的,而您却在Windows中编辑了本地文件(反之亦然)呢? Then, possibly, the line-endings would have changed. 然后,行尾可能会发生变化。 This would probably be completely invisible to you but would cause a merge conflict. 这对于您来说可能是完全不可见的,但是会导致合并冲突。 I've just tried it and got the following output: 我刚刚尝试过,并得到以下输出:

Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.

I then ran: 然后我跑了:

$ git mergetool
merge tool candidates: meld opendiff kdiff3 tkdiff xxdiff tortoisemerge gvimdiff diffuse ecmerge p4merge araxis bc3 codecompare emerge vimdiff
Merging:
file.txt

Normal merge conflict for 'file.txt':
  {local}: modified file
  {remote}: modified file
Hit return to start merge resolution tool (kdiff3):

Upon hitting return, kdiff3 silently merged the two files (I didn't even see it open) happily resolving the whitespace difference. 到达return时,kdiff3默默地合并了两个文件(我什至没有看到它打开),愉快地解决了空白差异。

That may not be exactly what is happening in your case, but it's a real example of Git failing to merge what kdiff3 auto-resolves simply because of whitespace. 这可能不是您所遇到的情况,但这是Git未能完全合并kdiff3仅由于空格而自动解析的示例的真实示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM