git pull报错:Auto Merge Failed; Fix Conflicts and Then Commit the Result.


本文来源:http://blog.csdn.net/trochiluses/article/details/10100719

1.出错场景:


协同开发时,咱们从远程服务器上pull下代码的时候,出现如下提示信息:git

Auto Merge Failed; Fix Conflicts and Then Commit the Result.
c#


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的结果能够发现:其中sources.list这个文件存在合并冲突

而进一步分析git pull的原理,实际上git pull是分了两步走的,(1)从远程pull下origin/master分支(2)将远程的origin/master分支与本地master分支进行合并app

以上的错误,是出在了第二步骤fetch


3.解决方法


方法一:若是咱们肯定远程的分支正好是咱们须要的,而本地的分支上的修改比较陈旧或者不正确,那么能够直接丢弃本地分支内容,运行以下命令(看须要决定是否须要运行git fetch取得远程分支):spa

$:git reset --hard origin/master.net

或者$:git reset --hard ORIG_HEADblog

解释:ip

 git-reset - Reset current HEAD to the specified state
ci

--hard
               Resets the index and working tree. Any changes to tracked files
               in the working tree since <commit> are discarded.

方法二:咱们不能丢弃本地修改,由于其中的某些内容的确是咱们须要的,此时须要对unmerged的文件进行手动修改,删掉其中冲突的部分,而后运行以下命令

$:git add filename

$:git commit -m "message"


方法三:若是咱们以为合并之后的文件内容比价混乱,想要废弃此次合并,回到合并以前的状态,那么能够运行以下命令:

$:git reset --hard HEAD


本文来源:http://blog.csdn.net/trochiluses/article/details/10100719