38.1 手动解决

在执行 git merge 时,你可能会遇到 Git 报告“合并冲突”错误。它会告诉你哪些文件存在冲突,你需要解决这些冲突。

在任何时候运行 git status 都会帮助你查看还需要编辑的内容,并给出提示信息,例如:

On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: index.html
no changes added to commit (use "git add" and/or "git commit -a")

Git 会在文件中留下标记,告诉你冲突发生的位置:

<<<<<<< HEAD: index.html # 表示当前分支的状态

<div id="footer">contact : email@somedomain.com</div>

======= # 表示冲突之间的分隔

<div id="footer">
please contact us at email@somedomain.com
</div>

>>>>>>> iss2: index.html # 表示其他分支(iss2)的状态

要解决冲突,你需要适当地编辑标记之间的内容,完全移除状态行(即 <<<<<<<、======= 和 >>>>>>> 行)。然后运行 git add index.html 标记冲突已解决,最后运行 git commit 完成合并。