1. 程式人生 > >git解決衝突與merge

git解決衝突與merge

                     

git衝突的場景與其他SCM工具一樣,我在這邊修改了檔案a,同事也修改了檔案a。同事比我先提交到倉庫中,那麼我pull程式碼時就會報錯:

$ git pullremote: Counting objects: 39, done.remote: Compressing objects: 100% (30/30), done.remote: Total 39 (delta 13), reused 0 (delta 0)Unpacking objects: 100% (39/39), done.From https://code.csdn.net/lincyang/androidwidgetdemo   d3b2814..5578
b8c  master     -> origin/masterUpdating d3b2814..5578b8cerror: Your local changes to the following files would be overwritten by merge:    app/src/main/AndroidManifest.xml    app/src/main/java/com/linc/skill/screenswitch/ScreenSwichActivity.javaPlease, commit your changes or stash them before you can merge.Aborting
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

而此時我又不顧這個錯誤,將我的程式碼add並commit,然後push時報如下錯:

To https://code.csdn.net/lincyang/androidwidgetdemo.git ! [rejected]        master -> master (non-fast-forward)error: failed to push some refs to 'https://code.csdn.net/lincyang/androidwidgetdemo.git'hint: Updates were rejected because the tip of
your current branch is behindhint: its remote counterpart. Integrate the remote changes (e.g.hint: 'git pull ...') before pushing again.hint: See the 'Note about fast-forwards' in 'git push --help' for details.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

然後我有執行了git pull:

$ git pullAuto-merging app/src/main/java/com/linc/skill/screenswitch/ScreenSwichActivity.javaCONFLICT (content): Merge conflict in app/src/main/java/com/linc/skill/screenswitch/ScreenSwichActivity.javaAuto-merging app/src/main/AndroidManifest.xmlCONFLICT (content): Merge conflict in app/src/main/AndroidManifest.xmlAutomatic merge failed; fix conflicts and then commit the result.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

那麼既然兩個檔案衝突,我就可以藉助mergetool來搞定它。我安裝了meld作為程式碼比對工具,那麼它理所當然也就成為我的mergetool了。

$ git mergetoolThis message is displayed because 'merge.tool' is not configured.See 'git mergetool --tool-help' or 'git help config' for more details.'git mergetool' will now attempt to use one of the following tools:meld opendiff kdiff3 tkdiff xxdiff tortoisemerge gvimdiff diffuse diffmerge ecmerge p4merge araxis bc3 codecompare emerge vimdiffMerging:app/src/main/AndroidManifest.xmlapp/src/main/java/com/linc/skill/screenswitch/ScreenSwichActivity.javaNormal merge conflict for 'app/src/main/AndroidManifest.xml':  {local}: modified file  {remote}: modified fileHit return to start merge resolution tool (meld): Normal merge conflict for 'app/src/main/java/com/linc/skill/screenswitch/ScreenSwichActivity.java':  {local}: modified file  {remote}: modified fileHit return to start merge resolution tool (meld): 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

merge完成後,執行git status發現有些檔案做了修改,那麼把這些檔案提交 吧,就把這次commit作為一次merge操作吧。

$ git commit -m "merge"[master 978aa1f] merge$ git pushCounting objects: 64, done.Delta compression using up to 4 threads.Compressing objects: 100% (25/25), done.Writing objects: 100% (33/33), 3.81 KiB | 0 bytes/s, done.Total 33 (delta 15), reused 0 (delta 0)To https://code.csdn.net/lincyang/androidwidgetdemo.git   5578b8c..978aa1f  master -> master$ git pullAlready up-to-date.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

至此,本次衝突解決完畢。

如果希望保留生產伺服器上所做的改動,僅僅併入新配置項, 處理方法如下:

git stashgit pullgit stash pop
  • 1
  • 2
  • 3