詳解git合併衝突解決方法
阿新 • • 發佈:2020-07-26
1、git merge衝突了,根據提示找到衝突的檔案,解決衝突
如果檔案有衝突,那麼會有類似的標記
2、修改完之後,執行git add 衝突檔名
3、git commit
注意:沒有-m選項
進去類似於vim的操作介面,把conflict相關的行刪除掉
4、直接push就可以了,因為剛剛已經執行過相關merge操作了
相關的操作如下
衝突產生
[root@Monitor Demo]# git branch #當前在master分支下 * master psr/psr-01 psr/psr-02 [root@Monitor Demo]# git checkout psr/psr-02 #切換到psr/psr-02分支下 Switched to branch 'psr/psr-02' [root@Monitor Demo]# git branch master psr/psr-01 * psr/psr-02 [root@Monitor Demo]# ls LICENSE new_remote_branch.txt psr_psr-02.txt README.md [root@Monitor Demo]# vim psr_psr-02.txt #修改psr/psr-02分支上的檔案 [root@Monitor Demo]# git add psr_psr-02.txt [root@Monitor Demo]# git commit -m 'psr_psr-02.txt has changed on psr/psr-02 branch' #提交到暫存區 [psr/psr-02 62ca72c] psr_psr-02.txt has changed on psr/psr-02 branch 1 files changed,6 insertions(+),0 deletions(-) [root@Monitor Demo]# git checkout master #切換到master分支下 Switched to branch 'master' [root@Monitor Demo]# vim psr_psr-02.txt #在master分支下也對psr_psr-02.txt進行修改 [root@Monitor Demo]# git add psr_psr-02.txt [root@Monitor Demo]# git commit -m 'changed this file on master branch' [master 282fbeb] changed this file on master branch 1 files changed,2 insertions(+),0 deletions(-) [root@Monitor Demo]# git merge psr/psr-02 #把psr/psr-02分支合併到當前分支,這時提示衝突了 Auto-merging psr_psr-02.txt CONFLICT (content): Merge conflict in psr_psr-02.txt Automatic merge failed; fix conflicts and then commit the result.
衝突解決過程
衝突檔案的格式基本如下
<<<<<<<到=======是在當前分支合併之前的檔案內容
=======到>>>>>>> psr/psr-02是在其它分支下修改的內容
需要在這個兩個版本中選擇一個,然後把標記符號也要一起刪除
<<<<<<< HEAD
add some lines on master branch add some lines on psr/psr-01 branch 2016年12月13日14:43:34 changed after psr/psr-02 ======= 1 2 3 4 5 >>>>>>> psr/psr-02
衝突檔案
vim psr_psr-02.txt <<<<<<< HEAD add some lines on master branch add some lines on psr/psr-01 branch 2016年12月13日14:43:34 changed after psr/psr-02 ======= 1 2 3 4 5 >>>>>>> psr/psr-02
修改衝突檔案
# vim psr_psr-02.txt README.md I'am in new branch psr/psr-02 based on psr/psr-01 add some lines on master branch add some lines on psr/psr-01 branch 2016年12月13日14:43:34 changed after psr/psr-02
新增衝突的檔案,然後就可以直接push了
Merge branch 'psr/psr-02' Conflicts: psr_psr-02.txt # # It looks like you may be committing a MERGE. # If this is not correct,please remove the file # .git/MERGE_HEAD # and try again. # # Please enter the commit message for your changes. Lines starting # with '#' will be ignored,and an empty message aborts the commit. # On branch master # Your branch is ahead of 'origin/master' by 1 commit. #
需要刪掉下面這兩行
Conflicts: psr_psr-02.txt
執行git push 操作
git push origin master
到此這篇關於詳解git合併衝突解決方法的文章就介紹到這了,更多相關git合併衝突內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!