人生不如意之事十之八九,合并分支往往也不是一帆风顺的。当然如果不会合并请看 创建和合并分支
准备新的feature1分支,继续我们的新分支开发:
asus@XXX MINGW64 /e/Git_warehouse (master)
$ git switch -c feature1
Switched to a new branch 'feature1'
asus@XXX MINGW64 /e/Git_warehouse (feature1)
$ git branch
* feature1
master
修改readme.txt最后一行,改为:
Creating a new branch is quick and simple.
在feature1分支上提交:
asus@XXX MINGW64 /e/Git_warehouse (feature1)
$ git add readme.txt
asus@XXX MINGW64 /e/Git_warehouse (feature1)
$ git commit -m "and simple"
[feature1 2809b02] and simple
1 file changed, 1 insertion(+), 1 deletion(-)
切换到master分支:
asus@XXX MINGW64 /e/Git_warehouse (feature1)
$ git switch master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
asus@XXX MINGW64 /e/Git_warehouse (master)
$ git branch
feature1
* master
在master分支上把readme.txt文件的最后一行改为:
Creating a new branch is quick & simple.
在master分支上提交readme.txt:
asus@XXX MINGW64 /e/Git_warehouse (master)
$ git add readme.txt
asus@XXX MINGW64 /e/Git_warehouse (master)
$ git commit -m "& simple"
[master 6edf4c3] & simple
1 file changed, 1 insertion(+), 1 deletion(-)
现在,master分支和feature1分支各自都分别有新的提交,变成了这样:
这种情况下,Git无法执行“快速合并”,只能试图把各自的修改合并起来,但这种合并就可能会有冲突,我们试试看:
$ git merge feature1
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.
果然冲突了!Git告诉我们,readme.txt文件存在冲突,必须手动解决冲突后再提交。git status也可以告诉我们冲突的文件:
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
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: readme.txt
no changes added to commit (use "git add" and/or "git commit -a")
我们可以直接查看 readme.txt 的内容:
Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容,我们修改如下(直接在Notepad++改 或者 vim改,去掉其他的就好):
Creating a new branch is quick and simple.
保存:
asus@XXX MINGW64 /e/Git_warehouse (master|MERGING)
$ git add readme.txt
asus@XXX MINGW64 /e/Git_warehouse (master|MERGING)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
(use "git push" to publish your local commits)
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: readme.txt
$ git commit -m "conflict fixed"
[master adc26a9] conflict fixed
现在,master分支和feature1分支变成了下图所示:
这时侯 git status,如下:
$ git status
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
当前master分支比远程的master分支要超前3个提交。
这时候 git log ,就可以发现这三次提交是什么?
用带参数的git log可以看到分支的合并情况,git log --graph --abbrev-commit(abbrev-commit是缩写commit的意思,可以不要;也可以加上–pretty=oneline,看简洁的,git log --graph --pretty=oneline --abbrev-commit):
最后删除feature1分支:
asus@XXX MINGW64 /e/Git_warehouse (master)
$ git branch -d feature1
Deleted branch feature1 (was 2809b02).
asus@XXX MINGW64 /e/Git_warehouse (master)
$ git branch
* master
当Git无法自动合并分支时,就必须首先解决冲突。解决冲突后,再提交,合并完成。
解决冲突就是把Git合并失败的文件手动编辑为我们希望的内容,再提交。
用git log --graph命令可以看到分支合并图。