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

查看:35
本文介绍了当相邻的行更改时,为什么 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 auto 不能解决这个问题,使用共同的祖先?如果我告诉 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天全站免登陆