git学习---冲突解决

多人协同开发时,常常会遇到多人修改同一个文件的情况,这时候提交代码,就会产生冲突的情况,在这里记录一下解决冲突的基本思路

 

先说说冲突的场景:

AB用的同一个版本v0的代码,第一个人A,修改了1.go,提交了代码,并且已合入主库,

这时候B在v0的基础上也修改了1.go ,提交了commit,在push的时候就遇到如下错误

hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决方案,

第一步,git pull

remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/Altruiste1/learnning_git
   ea1dc64..7f7273a  master     -> origin/master
Auto-merging 1.go
CONFLICT (content): Merge conflict in 1.go
Automatic merge failed; fix conflicts and then commit the result.

可以看到Merge conflict in 1.go,说明1.go这个文件产生了冲突,git不知道选择哪个版本,它对此保留了几个版本,

hello world
<<<<<<< HEAD
heo
=======
hello
>>>>>>> 7f7273a6b23124eb6ac81ec2aea31ba7eb911daa

然后修改这个文件,留下你想要的版本,保存后退出,然后执行这三步

git add 1.go

 git commit -m "modify 1.go"

git push

ok了

 

 

总结一下,解决冲突的方式,

1.git pull 拉取远程代码,这一步你可以看到冲突的文件

2.修改冲突的文件,1.go

3.git add *

4.git push  

实际生产环境不止一个branch,第一步和第四步根据实际情况来

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值