Git:解决合并冲突

时间:2015-07-25 02:56:04

标签: git version-control merge conflict

我采用以下示例来询问我在工作中遇到的合并冲突问题。

假设我在主分支中有一个名为 mainFile.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文件中:

branchA mainFile.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

branchB mainFile.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,但在我看来,我需要做出保留任何一个分支(而不是两个)的决定。

感谢您的帮助! enter image description here

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

首先,您可以将branchA合并到主服务器。之后,您将尝试从命令行将branchB合并到master,您将看到类似

的错误
  

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

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

  

<<<<<<< HEAD

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

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