当多个开发者同时修改同一个文件时,可能会引起冲突。Git 提供了一些工具来帮助你解决这些冲突。
以下是解决 Git 冲突的基本步骤:
在终端中进入你的 Git 项目目录。
运行 git status 命令,查看哪些文件存在冲突。冲突的文件将被标记为“both modified”。
$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
Changes to be committed:
modified: file1.txt
Unmerged paths:
(use "git add/rm <file>..." as appropriate to mark resolution)
both modified: file2.txt
- 打开包含冲突的文件,并寻找类似于下面的内容:
<<<<<<< HEAD
This is some content added by you.
=======
This is some content added by someone else.
>>>>>>> other-branch
- 解决冲突。在上面的示例中,你需要决定保留哪些内容。可以将文件修改为:
This is some content added by both of us.
保存并关闭文件。
运行 git add 命令,以标记文件已解决:
$ git add file2.txt
- 运行 git commit 命令提交更改:
$ git commit -m "Resolve conflict in file2.txt"
- 如果你已经解决了所有的冲突,运行 git push 命令将更改推送到远程仓库:
$ git push
如果解决冲突时遇到困难或需要更高级别的操作,可以参考 Git 文档中的更多信息。