【问题标题】:Why can't Git merge file changes with a modified parent/master?为什么 Git 不能将文件更改与修改的父/主合并?
【发布时间】:2011-02-05 00:56:50
【问题描述】:

我有一个文件,里面只有一行。 我创建了一个分支并将第二行添加到同一个文件中。保存并提交到分支。 我切换回主人。并在文件中添加不同的第二行。保存并提交给主人。 所以现在总共有 3 条独特的线路。

如果我现在尝试将分支合并回主分支,则会发生合并冲突。

为什么 Git 不能一个接一个地简单地合并每一行?

我的合并尝试是这样的:

PS D:\dev\testing\test1> git merge newbranch
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.
PS D:\dev\testing\test1> git diff
diff --cc hello.txt
index 726eeaf,e48d31a..0000000
--- a/hello.txt
+++ b/hello.txt
@@@ -1,2 -1,2 +1,6 @@@
  This is the first line.
- New line added by master.
 -Added a line in newbranch.
++<<<<<<< HEAD
++New line added by master.
++=======
++Added a line in newbranch.
++>>>>>>> newbranch

有没有办法让它一个接一个地自动插入?

【问题讨论】:

  • 似乎没有一种简单的方法来确定它们应该被插入的顺序 - 毕竟,在可执行代码方面,顺序当然很重要。

标签: git


【解决方案1】:

有一个名为 union 的 git 合并策略可以从 .gitattributes 配置文件进行配置,它会为您执行此操作,但您无法控制冲突行最终在合并中的顺序文件,并且没有冲突标记来指示发生的位置。

您还可以定义自定义合并策略,该策略可以使用有关文件结构的一些专业知识来确定正确的顺序。

【讨论】:

    【解决方案2】:

    假设你有这行:

    printf(something);
    

    在分支A,你成功了:

    while(x < 5)
    printf(something);
    

    在branchB中,你成功了:

    if(y>10)
    printf(something);
    

    你怎么能合并这个,或者更确切地说,你会相信一个工具为此创建的合并结果吗?

    【讨论】:

      【解决方案3】:

      假设文件分支 A 如下所示:

      First line
      Branch A's second line
      

      分支 B 看起来像:

      First line
      Branch B's second line
      

      当你合并时,有两种不同的方法来解决它。这是一种方法:

      First line
      Branch A's second line
      Branch B's second line
      

      这是另一种方式:

      First line
      Branch B's second line
      Branch A's second line
      

      Git 不知道您更喜欢这两个选项中的哪一个,或者是否可以接受合并。

      【讨论】:

        【解决方案4】:

        我可以告诉你为什么它不能合并两者 - 根据 git,FileA 的第 2 行是“这样那样”,而 FileB 的第 2 行是“其他东西”。因此,根据 git,它试图从 FileA 和 FileB 创建 File,其中一行具有两个可能的值。它无法决定你想要哪一个,所以它会将它们都转储并让你修复它。

        【讨论】:

          猜你喜欢
          • 2012-06-28
          • 2016-11-02
          • 2012-06-22
          • 1970-01-01
          • 1970-01-01
          • 2023-03-20
          • 1970-01-01
          • 2011-03-07
          • 2014-10-12
          相关资源
          最近更新 更多