简体   繁体   English

如果git可以弄清楚如何合并垂直(线条)更改,为什么不合并水平(字符)更改?

[英]If git can figure out how to merge vertical (line) changes, why not horizontal (character) changes?

If commit Apple puts 'a' on line 1 and commit Banana puts 'b' on line 3, git can just merge those commits together no problem, but if 'a' is at the beginning of line 1 and 'b' is at the end 9 characters across, it chokes and has no idea how to merge them and you have to do it manually. 如果提交Apple在行1上放置“ a”,而在香蕉行3上进行提交香蕉,则git可以将这些提交合并在一起没有问题,但是如果“ a”在行1的开头并且“ b”在行的开头跨过9个字符,它会窒息,不知道如何合并它们,您必须手动执行。

Why can't it figure this out? 为什么无法弄清楚呢?

Even if the same line is edited like this: 即使像这样编辑同一行:

original commit:       <question1 states="AZ,OH">
commit Orange line 25: <question1 states="AZ,IN,OH">
commit Pear line 25:   <question1 states="AZ,OH,TX">

Git merge can't figure out to put IN before OH and TX after, but I as a human can easily see how to do that, and I've used comparison editors (like KDiff3) that have pointed out for me which exact characters changed (color-coded), whereas git only shows changes by full lines. Git merge无法弄清楚在OH之前要放IN,TX之后要放,但是我作为一个人可以很容易地知道如何做到这一点,并且我使用了比较编辑器(如KDiff3)为我指出了确切的字符发生了变化(以颜色区分),而git仅以实线显示更改。

KDiff3 example: KDiff3示例: KDiff3_merge_conflict

I just can't tell if git thinks OH in Orange changed to IN,OH or if it's just too much processing to check if IN was only added before OH... but maybe there's a better merge tool that can figure this out? 我只是无法判断git认为Orange中的OH更改为IN,OH,或者是否进行了过多的处理以检查是否仅在OH之前添加了IN ...但是也许有一个更好的合并工具可以解决这个问题?

Git's built in merge algorithm uses Git's built-in diff, which is line oriented. Git的内置合并算法使用Git的内置diff(面向行)。 It's really that simple: it sees two diffs, one of which says "replace line A with line B" and one of which says "replace line A with line C" and the two replacements differ => conflict. 真的就是这么简单:它看到两个差异,其中一个说“用行B替换行A”,其中一个说“用行C替换行A”,两个替换不同=>冲突。

If you provide a custom merge driver, you can run your own diffs and do your own merging. 如果提供了自定义合并驱动程序,则可以运行自己的差异文件并进行自己的合并。 It's not at all trivial, though. 不过,这一点也不小。 In this particular case, a two-pass algorithm (first, find changed lines; second, see if changed lines can be handled as changed characters instead) might work. 在这种特殊情况下,可以使用两次遍历算法(第一,查找更改的行;第二,查看是否可以将更改的行作为更改的字符来处理)。

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

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