git分支01

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以后就不是合并状态了


总结

本文仅仅简单介绍了git分支合并的操作,分支合并中难免会遇到很多问题,还是需要组内成员积极协商解决。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值