6. 解决合并的冲突

优质
小牛编辑
118浏览
2023-12-01

把issue2分支和issue3分支的修改合并到master。

切换master分支后,与issue2分支合并。

$ git checkout master
Switched to branch 'master'
$ git merge issue2
Updating b2b23c4..8f7aa27
Fast-forward
 myfile.txt |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

执行fast-forward(快进)合并。

目前的历史记录

接着合并issue3分支。

$ git merge issue3
Auto-merging myfile.txt
CONFLICT (content): Merge conflict in myfile.txt
Automatic merge failed; fix conflicts and then commit the result.

自动合并失败。由于在同一行进行了修改,所以产生了冲突。这时myfile.txt的内容如下:

连猴子都懂的Git命令
add 把变更录入到索引中
<<<<<<< HEAD
commit 记录索引的状态
=======
pull 取得远端数据库的内容
>>>>>>> issue3

请修改

在发生冲突的地方,Git生成了内容的差异。请做以下修改:

连猴子都懂的Git命令
add 把变更录入到索引中
commit 记录索引的状态
pull 取得远端数据库的内容

修改冲突的部分,重新提交。

$ git add myfile.txt
$ git commit -m "合并issue3分支"
# On branch master
nothing to commit (working directory clean)

历史记录如下图所示。因为在这次合并中修改了冲突部分,所以会重新创建合并修改的提交记录。这样,master的HEAD就移动到这里了。这种合并不是fast-forward合并,而是non fast-forward合并。

目前的历史记录