git合併衝突解決方法

  • 一、git merge衝突了,根據提示找到衝突的文件,解決衝突

若是文件有衝突,那麼會有相似的標記git

  • 二、修改完以後,執行git add 衝突文件名vim

  • 三、git commit
    注意:沒有-m選項
    進去相似於vim的操做界面,把conflict相關的行刪除掉this

  • 四、直接push就能夠了,由於剛剛已經執行過相關merge操做了code

相關的操做以下rem

  • 衝突產生
[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

衝突文件it

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

修改衝突文件io

# 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.
#

須要刪掉下面這兩行ast

Conflicts:
    psr_psr-02.txt
  • 執行git push 操做
git push origin master
相關文章
相關標籤/搜索