当更改彼此相邻的行时,为什么git会产生合并冲突? [英] Why does git produce a merge conflict when lines next to each other are changed?

查看:164
本文介绍了当更改彼此相邻的行时,为什么git会产生合并冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在master中有一个具有此内容的文件:

Let's say I have a file with this content in master:

Line 1
Line 2
Line 3
Line 4

现在说我创建并签出一个名为test的新分支.在此分支中,我将文件更改为:

Now say I create and checkout a new branch called test. In this branch I change the file to this:

Line 1
Line 2
Line 3 Modified
Line 4

,然后我将其提交并切换回master.在master中,我将文件更改为:

and I commit this and switch back to master. In master I change the file to:

Line 1
Line 2
Line 3
Line 4 Modified

,我承诺.现在,如果我将分支test合并到master中,则会发生冲突.

and I commit. Now if I merge branch test into master, I get a conflict.

为什么git无法使用共同祖先自动解决此问题?如果我告诉git使用BeyondCompare作为difftool编辑冲突,则BeyondCompare会自动解决此问题,而无需告诉用户,因为这不是真正的冲突.有没有办法让git自动解决这些问题?我已经尝试过recursiveresolve合并策略,但是都没有.

Why can't git auto resolve this, using the common ancestor? If I tell git to edit conflicts using BeyondCompare as the difftool, BeyondCompare autoresolves this without even telling the user, since this isn't a real conflict. Is there a way to get git to autoresolve these? I've tried the recursive and resolve merge strategies but neither do it.

这是我们公司的问题,因为在某些文件中,多个开发人员会在附近更改行,这在拉动时会引起许多不必要的冲突.

It's an issue in our company because there are certain files where multiple developers change lines in close proximity and this causes many unnecessary conflicts when they pull.

推荐答案

此问题的答案很好地解释了Git如此行为的原因:

The reason that Git behaves like this is explained well in the answers to this question:

https://softwareengineering.stackexchange .com/questions/194788/why-doesnt-git-merge-adjacent-lines-without-conflict/378258#378258

本质上,因为您需要相邻的行来提供更改的上下文(不能仅使用行号,因为上面可能已添加或删除了某些内容),如果周围的行已更改,则通常不会希望Git天真地继续合并.用户Arsen7在该线程中给出了一个很好的例子,说明这可能会导致严重错误.

Essentially, because you need the neighboring lines to provide context to the change (you can't just use line numbers, because something may have been added or deleted above), if the lines around it have changed you usually don't want Git to just naively continue with the merge. User Arsen7 gives a good example in that thread of how this could go badly wrong.

但是,我同意您的观点,有时候这很烦人,因此我编写了一个自定义合并驱动程序,可以解决合并/重新设置期间的此类冲突.它被设计为交互式的,因为我一直想在继续进行之前检查它是否做对了,但是如果您确信它会起作用,则可以轻松地对其进行修改.

However, I agree with you that sometimes this is quite annoying, so I wrote a custom merge driver that can resolve such conflicts during merging/rebasing. It's designed to be interactive, because I always want to check that it's going to do the right thing before going ahead, but you could easily modify it not to be if you're confident it's going to work.

如果您有兴趣,可以在GPLv3 +许可下在GitHub上使用该脚本:

If you're interested, the script is available on GitHub under a GPLv3+ license:

https://github.com/paulaltin/git-subline-merge

这篇关于当更改彼此相邻的行时,为什么git会产生合并冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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