git系列文章目录
第二章 分支冲突问题,以及如何解决冲突文章目录
前言
分工合作难免会遇到代码冲突,这些冲突是如何产生的,以及如何解决一、分支的相互不影响
接上一篇笔记在创建了分支branch01后修改test4.txt内容然后提交到暂存区
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (branch01)
$ git add test4.txt
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (branch01)
$
提交到本地库
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (branch01)
$ git commit -m "在分支01的test4.txt中增加内容" test4.txt
[branch01 408be95] 在分支01的test4.txt中增加内容
1 file changed, 2 insertions(+), 1 deletion(-)
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (branch01)
$
再将分支切换到master
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (branch01)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 14 commits.
(use "git push" to publish your local commits)
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git branch -v
branch01 408be95 在分支01的test4.txt中增加内容
* master c2b6b4c [ahead 14] 创建添加了test4.txt,用来演示分支
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$
此时*号在master前,说明已经切换到maser分支了。注意两个分支的索引号
这个时候再将主分支的test4.txt修改并提交
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ cat test4.txt
aaabbb
cccc
ddd
增加内容by master
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git add test4.txt
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git commit -m "主分支master中修改了test4.txt中的内容" test4.txt
[master 2388116] 主分支master中修改了test4.txt中的内容
1 file changed, 2 insertions(+), 1 deletion(-)
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$
再切换到branch01中查看分支状态
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git checkout branch01
Switched to branch 'branch01'
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (branch01)
$ git branch -v
* branch01 408be95 在分支01的test4.txt中增加内容
master 2388116 [ahead 15] 主分支master中修改了test4.txt中的内容
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (branch01)
$
二、分支的合并
1.将branch01分支合并到master主分支
首先切换到主分支(示例):
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (branch01)
$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 15 commits.
(use "git push" to publish your local commits)
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$
2.将branch01分支合并到master主分支
代码如下(示例):
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$ git merge branch01
Auto-merging test4.txt
CONFLICT (content): Merge conflict in test4.txt
Automatic merge failed; fix conflicts and then commit the result.
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master|MERGING)
$
此处提示Merge conflict in test4.txt,说明分支合并有冲突。
查看test4.txt发现文件发生了改变
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master|MERGING)
$ cat test4.txt
aaabbb
cccc
ddd
<<<<<<< HEAD
增加内容by master
=======
增加内容by branch01
>>>>>>> branch01
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master|MERGING)
$
三、分支的冲突
1.什么时候出现冲突
经过上面的代码合并例子说明,在不同分支的同一个文件的同一个位置进行修改后的合并会发生冲突
2.如何解决冲突
公司内部商议解决,或者自己决定 人为决定,留下想要的即可,不要的直接删除就好了
注意:git不会帮你删除代码,需要人工处理。
人工商议后修改test4.txt
将test4.txt提交到暂存区
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master|MERGING)
$ cat test4.txt
aaabbb
cccc
ddd
增加内容by branch01
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master|MERGING)
$ git add test4.txt
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master|MERGING)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 15 commits.
(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: test4.txt
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master|MERGING)
$
显示冲突已经被修复,但仍然处于合并状态中
然后提交到本地仓库,这里需要注意的是,提交的时候不要带文件名,否则提交合并不成功
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master|MERGING)
$ git commit -m "解决了test4.txt的合并冲突问题,并提交到本地仓库" test4.txt
fatal: cannot do a partial commit during a merge.
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master|MERGING)
$ git commit -m "解决了test4.txt的合并冲突问题,并提交到本地仓库"
[master 8f53cbd] 解决了test4.txt的合并冲突问题,并提交到本地仓库
Apple@DESKTOP-ECLNIU2 MINGW64 ~/Desktop/git basic operation/git-basic (master)
$
commit以后就不是合并状态了