【Git入门之九】解决冲突

原创作品,转载请标明:http://blog.csdn.net/jackystudio/article/details/12309531


1.多人协作冲突

如果多人同时修改了同一个文件,那会出现什么样的结果呢?我们试着这么做。


(1)修改jackygit2

在jackygit2中修改jackydata01,提交修改并推送到远程仓库(这里使用本地远程仓库)。正常,没问题。

[cpp]  view plain copy
  1. #切换到jackygit2本地库,这是远程仓库那节建立的,从本地远程仓库克隆而来  
  2. $ cd ../jackygit2  
  3.   
  4. #修改jackygit2/Jackydata01  
  5. $ echo "It's modified in jackygit2" > jackydata01  
  6.   
  7. #提交jackygit2的修改  
  8. $ git commit -a -m "jackygit2 modify"  
  9. [master 15a6406] jackygit2 modify  
  10.  1 file changed, 1 insertion(+), 1 deletion(-)  
  11.   
  12. #推送到远程仓库origin  
  13. $ git push origin  
  14. Counting objects: 5, done.  
  15. Delta compression using up to 2 threads.  
  16. Compressing objects: 100% (2/2), done.  
  17. Writing objects: 100% (3/3), 283 bytes | 0 bytes/s, done.  
  18. Total 3 (delta 1), reused 0 (delta 0)  
  19. To d:/jackygit2/../remote-jackygit.git  
  20.    c0449de..15a6406  master -> master  
(2)修改jackygit

切换到jackygit仓库,同样修改jackydata01,提交修改并推送到远程仓库。这时候就出错了,提示有人改过这个文件了。推送不成功。

[cpp]  view plain copy
  1. #切换到jackygit  
  2. $ cd ../jackygit  
  3.   
  4. #修改jackygit/Jackydata01  
  5. $ echo "It's modified in jackygit" >jackydata01  
  6.   
  7. #提交jackygit的修改  
  8. $ git commit -a -m "jackygit modify"  
  9. [master 9ab7206] jackygit modify  
  10.  1 file changed, 1 insertion(+), 1 deletion(-)  
  11.   
  12. 推送到远程仓库testremote,这个仓库和上面的origin是同一个远程仓库  
  13. $ git push testremote  
  14. To ../remote-jackygit.git  
  15.  ! [rejected]        master -> master (fetch first)  
  16. error: failed to push some refs to '../remote-jackygit.git'  
  17. hint: Updates were rejected because the remote contains work that you do  
  18. hint: not have locally. This is usually caused by another repository pushing  
  19. hint: to the same ref. You may want to first integrate the remote changes  
  20. hint: (e.g., 'git pull ...') before pushing again.  
  21. hint: See the 'Note about fast-forwards' in 'git push --help' for details.  
(3)抓取远程仓库内容

抓取远程仓库中的文件,这里需要指定分支。

[cpp]  view plain copy
  1. #抓取远程仓库中的文件,需要指定分支  
  2. $ git pull testremote master  
  3. From ../remote-jackygit  
  4.  * branch            master     -> FETCH_HEAD  
  5. Auto-merging Jackydata01  
  6. CONFLICT (content): Merge conflict in Jackydata01  
  7. Automatic merge failed; fix conflicts and then commit the result.  
(4)查看冲突文件内容

冲突用<<<<<< ====== >>>>>>隔开冲突代码,上面的是当前修改内容,下面的则是别人修改的内容。

[cpp]  view plain copy
  1. #输出当前jackydata01内容  
  2. $ cat jackydata01  
  3. <<<<<<< HEAD  
  4. It's modified in jackygit  
  5. =======  
  6. It's modified in jackygit2  
  7. >>>>>>> 15a6406ed7f889bab7e812f9e6bedb6e78431232  
(5)解决冲突

修改冲突内容,重新提交,推送。

[cpp]  view plain copy
  1. #修改Jackydata01产生的冲突  
  2. $ echo "there is no conflict now" > jackydata01  
  3.   
  4. #提交修改冲突后文件  
  5. $ git commit -a -m "there is no confilct now"  
  6. [master ac2846f] there is no confilct now  
  7.   
  8. #推送到远程仓库testremote  
  9. $ git push testremote  
  10. Counting objects: 10, done.  
  11. Delta compression using up to 2 threads.  
  12. Compressing objects: 100% (4/4), done.  
  13. Writing objects: 100% (6/6), 567 bytes | 0 bytes/s, done.  
  14. Total 6 (delta 2), reused 0 (delta 0)  
  15. To ../remote-jackygit.git  
  16.    15a6406..ac2846f  master -> master  

2.分支合并冲突

这种情况和多人协作冲突处理办法是相似的。在这里也模拟一下。


(1)创建分支并修改内容

[cpp]  view plain copy
  1. #创建并跳转到分支br  
  2. $ git checkout -b br  
  3. Switched to a new branch 'br'  
  4.   
  5. #修改br分支中的Jackydata01  
  6. $ echo "It's modified in br" > jackydata01  
  7.   
  8. #提交br分支中的修改  
  9. $ git commit -a -m "br modify"  
  10. [br 32c8755] br modify  
  11.  1 file changed, 1 insertion(+), 1 deletion(-)  
(2)跳转到主分支,同样进行内容修改
[cpp]  view plain copy
  1. #跳转到master分支  
  2. $ git checkout master  
  3. Switched to branch 'master'  
  4.   
  5. #修改master分支中的jackydata01  
  6. $ echo "It's modified in master" > jackydata01  
  7.   
  8. #提交master分支中的修改  
  9. $ git commit -a -m "master modify"  
  10. [master ed84e67] master modify  
  11.  1 file changed, 1 insertion(+), 1 deletion(-)  
(3)合并分支
[cpp]  view plain copy
  1. #合并br到主分支上  
  2. $ git merge br  
  3. Auto-merging Jackydata01  
  4. CONFLICT (content): Merge conflict in Jackydata01  
  5. Automatic merge failed; fix conflicts and then commit the result.  
(4)显示冲突文件内容,冲突隔开方式如上所述
[cpp]  view plain copy
  1. #显示br中Jackydata01内容  
  2. $ cat jackydata01  
  3. <<<<<<< HEAD  
  4. It's modified in master  
  5. =======  
  6. It's modified in br  
  7. >>>>>>> br  
(5)解决冲突,重新提交
[cpp]  view plain copy
  1. 修改Jackydata01产生的冲突  
  2. $ echo "It's no conflict now" > jackydata01  
  3.   
  4. #提交修改冲突后文件  
  5. $ git commit -a -m "It's no conflict now"  
  6. [master eb073a6] It's no conflict now  
(6)删除无效分支
[cpp]  view plain copy
  1. #删除br分支  
  2. $ git branch -d br  
  3. Deleted branch br (was 32c8755).  
  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值