【问题标题】:Git: resolving merge conflictsGit:解决合并冲突
【发布时间】:2015-10-15 19:52:20
【问题描述】:

我以下面的例子来问一个我在工作中遇到的合并冲突问题。

假设我在ma​​ster分支中有一个名为ma​​inFile.txt的文件,其内容为:

  1 this is the first line in master branch
  2 this is the second line in master branch
  3 this is the third line in master branch

从 master 分支,我创建了两个分支:branchA 和 branchB,每个分支都有新行(从两个分支的第 4 行开始)添加到同一个 mainFile.txt 文件中:

branchAma​​inFile.txt的内容:

  1 this is the first line in master branch
  2 this is the second line in master branch
  3 this is the third line in master branch
  4 This is the fourth line in branchA
  5 this is the fifth line in branchA

branchBma​​inFile.txt的内容:

  1 this is the first line in master branch
  2 this is the second line in master branch
  3 this is the third line in master branch
  4 This is the fourth line in branchB
  5 This is the fifth line in branchB
  6 This is the sixth line in branchB

现在,我需要将两个分支合并回 master。当我先将branchA合并到master时没有问题。但是,当我将 branchB 合并到 master 时,会出现合并冲突。从以下附加的合并冲突屏幕截图中,我需要保留 branchA 和 branchB 中的所有行。我想知道如何解决合并冲突。我一直在尝试使用 vimdiff,但在我看来,我需要决定保留任一分支(而不是两者)。

感谢您的帮助!

【问题讨论】:

    标签: git version-control merge conflict


    【解决方案1】:

    最好使用任何 3 路合并工具,例如 meld http://meldmerge.org/,它还考虑了这两个分支的共同祖先,这意味着您更容易识别更改并通过合并工作。使用 vi 或文本编辑器来合并大的和冲突的代码 sn-p 变得很困难,因为如果不向下滚动就无法在同一屏幕中看到比较。当您有冲突时,您可以使用git mergetool 启动合并时选择保留两个分支

    【讨论】:

    • 是的,为此您绝对需要一个适当的 3 路合并程序。我使用KDiff3,对此我很满意。
    【解决方案2】:

    首先你可以将 branchA 合并到 master。之后,当您尝试从命令行将 branchB 合并到 master 时,您会看到类似

    的错误

    自动合并失败;修复冲突,然后提交结果

    现在打开 branchB 的 mainFile.txt。您将得到一行代码

    由于此行下方的代码,您的分支存在冲突。根据您的最终要求手动更改您的代码。

    之后添加、提交并推送您的 mainFile.txt。在此之后,您应该能够解决您的冲突。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-19
      • 2018-08-04
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 2012-03-04
      相关资源
      最近更新 更多