Git冲突处理

网友投稿 555 2022-10-09

Git冲突处理

Git冲突处理

Git冲突很常见。本质是两个分支之间都提交了相同的东西,如:

同时修改了同一份文件创建了相同的文件

解决冲突的办法:

手动合并两个文件,各自再提交,然后再进行合并选择保留其中一份文件,删除掉另一份

同时修改了同一份文件引起的冲突及其解决方法例子:

# 切换到master分支~/Desktop/MyApp$ git checkout statusOn branch masternothing to commit, working tree clean~/Desktop/MyApp$ lsapp H.txt# 创建两个分支~/Desktop/MyApp$ git branch tt01~/Desktop/MyApp$ git branch tt02# 修改tt01分支的H.txt文件~/Desktop/MyApp$ git checkout tt01Switched to branch 'tt01'~/Desktop/MyApp$ lsapp H.txt~/Desktop/MyApp$ echo hello > H.txt~/Desktop/MyApp$ git add H.txt~/Desktop/MyApp$ git commit H.txt -m "init H.txt"~/Desktop/MyApp$ cat H.txthello# 修改tt02分支的H.txt文件~/Desktop/MyApp$ git checkout tt02~/Desktop/MyApp$ lsapp H.txt~/Desktop/MyApp$ echo hi > H.txt~/Desktop/MyApp$ git add H.txt~/Desktop/MyApp$ git commit H.txt -m "init H.txt"~/Desktop/MyApp$ cat H.txthi# 将分支tt02合并到分支tt01~/Desktop/MyApp$ git checkout tt01Switched to branch 'tt01'# 合并tt02到tt01过程中,出现冲突~/Desktop/MyApp$ git merge tt02Auto-merging H.txtCONFLICT (content): Merge conflict in H.txtAutomatic merge failed; fix conflicts and then commit the result.~/Desktop/MyApp$ cat H.txt<<<<<<< HEADhello=======hi>>>>>>> tt02~/Desktop/MyApp$ git statusOn branch tt01You 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: H.txtno changes added to commit (use "git add" and/or "git commit -a")# 解决冲突,手动合并,将两个分支手动合并,然后各自commit,最后再合并分支# 修改tt01分支的H.txt文件~/Desktop/MyApp$ git checkout tt01Switched to branch 'tt01'~/Desktop/MyApp$ echo hello,hi > H.txt~/Desktop/MyApp$ git add H.txt~/Desktop/MyApp$ git commit H.txt -m "init H.txt"~/Desktop/MyApp$ cat H.txthello,hi# 修改tt02分支的H.txt文件~/Desktop/MyApp$ git checkout tt02~/Desktop/MyApp$ echo hello,hi > H.txt~/Desktop/MyApp$ git add H.txt~/Desktop/MyApp$ git commit H.txt -m "init H.txt"~/Desktop/MyApp$ cat H.txthello,hi# 再次合并~/Desktop/MyApp$ git checkout tt01Switched to branch 'tt01'# 合并成功~/Desktop/MyApp$ git merge tt02Merge made by the 'recursive' strategy.

创建了相同的文件引起的冲突及其解决方法例子:

# 切换到master分支~/Desktop/MyApp$ git checkout masterSwitched to branch 'master'# 创建分支test01 、test02~/Desktop/MyApp$ git branch test01~/Desktop/MyApp$ git branch test02# 在test01分支创建一个test.txt文件~/Desktop/MyApp$ git checkout test01Switched to branch 'test01'~/Desktop/MyApp$ touch test.txt~/Desktop/MyApp$ echo Hello test01>test.txt~/Desktop/MyApp$ git add test.txt~/Desktop/MyApp$ git commit test.txt -m "init test.txt of test01"[test01 b323e2c] init test.txt of test01 1 file changed, 1 insertion(+) create mode 100644 test.txt# 在test02分支创建一个test.txt文件~/Desktop/MyApp$ git checkout test02Switched to branch 'test02'~/Desktop/MyApp$ touch test.txt~/Desktop/MyApp$ echo hello test02 > test.txt~/Desktop/MyApp$ git add test.txt~/Desktop/MyApp$ git commit test.txt -m "init test"[test02 fd1e225] init test 1 file changed, 1 insertion(+) create mode 100644 test.txt# 将分支test01合并到test02~/Desktop/MyApp$ git merge test01Auto-merging test.txtCONFLICT (add/add): Merge conflict in test.txtAutomatic merge failed; fix conflicts and then commit the result.# 通过git status可以查看冲突信息~/Desktop/MyApp$ git statusOn branch test02You 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 added: test.txtno changes added to commit (use "git add" and/or "git commit -a")# 要先终止合并,才能去做别的事~/Desktop/MyApp$ git merge --abort~/Desktop/MyApp$ git statusOn branch test02nothing to commit, working tree clean~/Desktop/MyApp$ lsapp H.txt test.txt# 删除test02分支的test.txt文件,使用test01分支的test.txt文件~/Desktop/MyApp$ git rm test.txtrm 'test.txt'~/Desktop/MyApp$ git commit -m "delete test.txt> "[test02 49c01a2] delete test.txt 1 file changed, 1 deletion(-) delete mode 100644 test.txt# 再次合并~/Desktop/MyApp$ git merge test01Merge made by the 'recursive' strategy. test.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 test.txt

谢谢阅读!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序示例教程(任务列表)(微信小程序开发教程手册)
下一篇:大型鱼类数据集
相关文章

 发表评论

暂时没有评论,来抢沙发吧~