文章内容

2017/1/22 22:09:55,作 者: 黄兵

git pull提示fix conflicts and then commit the result

出现错误场景:

有一个项目,在同一个分支下面,A修改了之后提交,B修改了之后没有提交,之后B拉取A提交的,产生冲突。

因为B修改了之后没有提交,与A产生冲突,之后无法同步。

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

warning: Cannot merge binary files: WebApplication1/Images/logo.png (HEAD vs. 3c
372fd139810c244de39dcc58a30c1a37e5be5c)

注释:警告:不能合并二进制文件
Auto-merging WebApplication1/Images/logo.png
CONFLICT (content): Merge conflict in WebApplication1/Images/logo.png
Auto-merging BlogAppDAL/Entities/SEO.cs
Auto-merging BlogAppDAL/BlogAppDAL.csproj
CONFLICT (content): Merge conflict in BlogAppDAL/BlogAppDAL.csproj
Automatic merge failed; fix conflicts and then commit the result.

注释:自动合并失败; 修复冲突,然后提交结果。

2.原因分析:

    利用git status,输出如下:

On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)
Changes to be committed:

注释:

在主分支

你的分支和'origin / master'已经产生分歧,

并且分别具有1和1个不同的提交。

   (使用“git pull”将远程分支合并到您的分支)

您有未合并的路径。

   (修复冲突并运行“git commit”)

   (使用“git merge --abort”中止合并)

要提交的更改:


        modified:   BlogAppDAL/Entities/Blog.cs
        modified:   BlogAppDAL/Entities/SEO.cs
        new file:   BlogAppDAL/Migrations/201701220550350_2016-1-22.Designer.cs
        new file:   BlogAppDAL/Migrations/201701220550350_2016-1-22.cs
        new file:   BlogAppDAL/Migrations/201701220550350_2016-1-22.resx
        modified:   WebApplication1/Controllers/ManageController.cs
        modified:   WebApplication1/Models/BlogAddArtViewModel.cs
        modified:   WebApplication1/Views/Manage/AddArticle.cshtml

Unmerged paths:

注释:未合并路径(无法合并):
  (use "git add <file>..." to mark resolution)
        both modified:   BlogAppDAL/BlogAppDAL.csproj
        both modified:   WebApplication1/Images/logo.png

  从git status的结果可以发现:其中BlogAppDAL.csproj和logo.png这个文件存在合并冲突

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

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

    3.解决方法

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

  $: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 are discarded.

 方法二:我们不能丢弃本地修改,因为其中的某些内容的确是我们需要的,此时需要对unmerged的文件进行手动修改,删掉其中冲突的部分,然后运行如下命令

    $:git add filename

    $:git commit -m "message"

如果有多个文件可以多次运行  $:git add filename  命令。

    方法三:如果我们觉得合并以后的文件内容比较混乱,想要废弃这次合并,回到合并之前的状态,那么可以运行如下命令:

    $:git reset --hard HEAD

参考资料:爱学艺-经验 教程

分享到:

发表评论

评论列表

user-ico

CCC on 回复 有用(2

解决了我的问题,写的哼详细。