1.出錯(cuò)場景:
協(xié)同開發(fā)時(shí),我們從遠(yuǎn)程服務(wù)器上pull下代碼的時(shí)候,出現(xiàn)以下提示信息:
Auto Merge Failed; Fix Conflicts and Then Commit the Result.
2.原因分析:
利用git status,輸出如下:
root@hyk-virt:/etc# git status# On branch master# Your branch and 'origin/master' have diverged,# and have 2 and 2 different commits each, respectively.## Unmerged paths:# (use "git add/rm <file>..." as appropriate to mark resolution)## both modified: apt/sources.list## Changes not staged for commit:# (use "git add <file>..." to update what will be committed)# (use "git checkout -- <file>..." to discard changes in working directory)## modified: cups/subscriptions.conf# modified: cups/subscriptions.conf.O# modified: mtab# modified: update-manager/release-upgrades#no changes added to commit (use "git add" and/or "git commit -a")
從git status的結(jié)果可以發(fā)現(xiàn):其中sources.list這個(gè)文件存在合并沖突
而進(jìn)一步分析git pull的原理,實(shí)際上git pull是分了兩步走的,(1)從遠(yuǎn)程pull下origin/master分支(2)將遠(yuǎn)程的origin/master分支與本地master分支進(jìn)行合并
以上的錯(cuò)誤,是出在了第二步驟
3.解決方法
方法一:如果我們確定遠(yuǎn)程的分支正好是我們需要的,而本地的分支上的修改比較陳舊或者不正確,那么可以直接丟棄本地分支內(nèi)容,運(yùn)行如下命令(看需要決定是否需要運(yùn)行g(shù)it fetch取得遠(yuǎn)程分支):
$:git reset --hard origin/master
或者$:git reset --hard ORIG_HEAD
解釋:
git-reset - Reset current HEAD to the specified state
--hard Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.
方法二:我們不能丟棄本地修改,因?yàn)槠渲械哪承﹥?nèi)容的確是我們需要的,此時(shí)需要對unmerged的文件進(jìn)行手動(dòng)修改,刪掉其中沖突的部分,然后運(yùn)行如下命令
$:git add filename
$:git commit -m "message"
方法三:如果我們覺得合并以后的文件內(nèi)容比價(jià)混亂,想要廢棄這次合并,回到合并之前的狀態(tài),那么可以運(yùn)行如下命令:
$:git reset --hard HEAD