多人协同开发时,常常会遇到多人修改同一个文件的情况,这时候提交代码,就会产生冲突的情况,在这里记录一下解决冲突的基本思路
先说说冲突的场景:
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,第一步和第四步根据实际情况来