zoukankan      html  css  js  c++  java
  • 【Git入门之九】解决冲突

    【Git入门之九】解决冲突 - JackyStudio - 博客频道 - CSDN.NET

    1.多人协作冲突

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



    (1)修改jackygit2

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

    [cpp] view
    plain
    copy
     
    1. #切换到jackygit2本地库,这是远程仓库那节建立的,从本地远程仓库克隆而来  
    2. $Snbsp;cd ../jackygit2  
    3.   
    4. #修改jackygit2/Jackydata01  
    5. $Snbsp;echo "It's modified in jackygit2" > jackydata01  
    6.   
    7. #提交jackygit2的修改  
    8. $Snbsp;git commit -a -m "jackygit2 modify"  
    9. [master 15a6406] jackygit2 modify  
    10.  1 file changed, 1 insertion(+), 1 deletion(-)  
    11.   
    12. #推送到远程仓库origin  
    13. $Snbsp;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. $Snbsp;cd ../jackygit  
    3.   
    4. #修改jackygit/Jackydata01  
    5. $Snbsp;echo "It's modified in jackygit" >jackydata01  
    6.   
    7. #提交jackygit的修改  
    8. $Snbsp;git commit -a -m "jackygit modify"  
    9. [master 9ab7206] jackygit modify  
    10.  1 file changed, 1 insertion(+), 1 deletion(-)  
    11.   
    12. 推送到远程仓库testremote,这个仓库和上面的origin是同一个远程仓库  
    13. $Snbsp;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. $Snbsp;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. $Snbsp;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. $Snbsp;echo "there is no conflict now" > jackydata01  
    3.   
    4. #提交修改冲突后文件  
    5. $Snbsp;git commit -a -m "there is no confilct now"  
    6. [master ac2846f] there is no confilct now  
    7.   
    8. #推送到远程仓库testremote  
    9. $Snbsp;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)创建分支并修改内容

     
    1. #创建并跳转到分支br  
    2. $Snbsp;git checkout -b br  
    3. Switched to a new branch 'br'  
    4.   
    5. #修改br分支中的Jackydata01  
    6. $Snbsp;echo "It's modified in br" > jackydata01  
    7.   
    8. #提交br分支中的修改  
    9. $Snbsp;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. $Snbsp;git checkout master  
    3. Switched to branch 'master'  
    4.   
    5. #修改master分支中的jackydata01  
    6. $Snbsp;echo "It's modified in master" > jackydata01  
    7.   
    8. #提交master分支中的修改  
    9. $Snbsp;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. $Snbsp;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. $Snbsp;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. $Snbsp;echo "It's no conflict now" > jackydata01  
    3.   
    4. #提交修改冲突后文件  
    5. $Snbsp;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. $Snbsp;git branch -d br  
    3. Deleted branch br (was 32c8755).  
  • 相关阅读:
    【BZOJ3993】星际战争(SDOI2015)-二分答案+最大流
    【BZOJ3996】线性代数(TJOI2015)-最小割
    【BZOJ3996】线性代数(TJOI2015)-最小割
    【APIO2010T2】巡逻-贪心+树形DP
    【APIO2010T2】巡逻-贪心+树形DP
    【NOIP2016提高组T2】天天爱跑步-倍增LCA+树上差分
    【NOIP2016提高组T2】天天爱跑步-倍增LCA+树上差分
    【POJ2411】Mondriaan's Dream-状态压缩DP(插头DP?)
    【POJ2411】Mondriaan's Dream-状态压缩DP(插头DP?)
    【POJ1679】The Unique MST-次小生成树(判断最小生成树唯一性)
  • 原文地址:https://www.cnblogs.com/seven1979/p/4257310.html
Copyright © 2011-2022 走看看