Skip to main content
  1. Learn center
  2. Software Development
  3. Guides
  4. Git tutorial
  5. 如何使用 Git
  6. 如何在 Git 中使用分支
  7. 解决合并的冲突
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

解决合并的冲突

接下来,让我们把我们在上个步骤创建的issue2issue3分支,合并到主分支。

首先,切换到main,并使用快进合并,来合并issue2

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

现在的历史记录如下:

Current history

接下来,尝试将issue3合并到main中。

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

在这种情况下,Git 已识别出冲突,因为myfile.txt文件在每个分支上的同一行中具有不同的内容,并且无法自动将issue3main合并。

myfile.txt文件现在看起来像这样:

Anyone can learn Git with this tutorial and Backlog
add: Register a change in an index
<<<<<<< HEAD
commit: Save the status of an index
=======
pull: Obtain the content of a remote repository
>>>>>>> issue3

您会发现 Git 添加到myfile.txt文件的标记,以及有关冲突的信息。我们必须手动修复冲突,如下所示。

Anyone can learn Git with this tutorial and Backlog
add: Register a change in an index
commit: Save the status of an index
pull: Obtain the content of a remote repository

解决冲突后,提交更改。

$ git add myfile.txt
$ git commit -m "merge issue3 branch"
# On branch main
nothing to commit (working directory clean)

历史记录将如下所示:

Current history

您可以看到,由于冲突的解决,一个新的合并提交已经被创建。主分支现在指向最新的合并提交。这是一个非快进合并。

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life