HOME
NAVIGATION

探索Git(6)——解决分支冲突

☞探索git的合集

添加一个新的分支feature1

$ git checkout -b feature1
Switched to a new branch 'feature1'

把test.docx的最后一句修改为Creating a new branch is quick AND simple.

然后使用git add 和git commit -m"AND simple"提交到版本库

切换到master git checkout master,并把test.docx最后一句修改为Creating a new branch is quick & simple.并提交到版本库

现在,master分支和feature1分支各自都分别有新的提交,变成了这样:

这种情况下,Git无法执行“快速合并”,只能试图把各自的修改合并起来,但这种合并就可能会有冲突

$ git merge feature1
warning: Cannot merge binary files: test.docx (HEAD vs. feature1)
Auto-merging test.docx
CONFLICT (content): Merge conflict in test.docx
Automatic merge failed; fix conflicts and then commit the result.

果然出现了冲突!

Git告诉我们,test.docx文件存在冲突,必须手动解决冲突后再提交。git status也可以告诉我们冲突的文件:

$ git status
On branch master
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)
Unmerged paths:
(use "git add ..." to mark resolution)
both modified: test.docx
no changes added to commit (use "git add" and/or "git commit -a")

然后就乖乖的把master的改过来吧,然后提交

$ git add test.docx
$ git commit -m"conflict fixed"
[master 88adefa] conflict fixed

现在,master分支和feature1分支变成了下图所示:

然后惯例删除分支feature1

$ git branch -d feature1
Deleted branch feature1 (was b7d4aea).

大功告成!