我如何在我的 Git 存储库中解决合并冲突?


当前回答

对于使用 Visual Studio 的用户(Visual Studio 2015 在我的情况下)

关闭您的项目在Visual Studio. 特别是在大型项目,Visual Studio倾向于在使用UI合并时脱离。 立即在命令中进行合并. git checkout target_branch git merge source_branch 然后在Visual Studio中打开项目,然后转到Team Explorer → Branch。

其他回答

如果您正在使用 IntelliJ IDEA 作为 IDE,请尝试通过:

git checkout <localbranch>
git merge origin/<remotebranch>

它将显示所有类似的冲突:

A_MBPro:test anu$ git merge 起源/ 自动合并 src/test/java/com/.../TestClass.java CONFLICT (内容): 合并冲突 src/test/java/com/.../TestClass.java

现在请注意,TestClass.java 文件在 IntelliJ IDEA 中显示为红色。

此外,Git 状态也将显示:

Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified:   src/test/java/com/.../TestClass.java

在 IntelliJ IDEA 中打开文件,将有与

  <<<<<<< HEAD
    public void testMethod() {
    }
    =======
    public void testMethod() { ...
    }
    >>>>>>> origin/<remotebranch>

在哪里,头部是你的本地分支和起源的变化/<remotebranch>是从远方分支的变化. 在这里保持你需要的东西,并删除你不需要的东西。

   git add TestClass.java
   git commit -m "commit message"
   git push

查看 Stack Overflow 问题中的答案 堕胎在 Git 中的合并,特别是 Charles Bailey 的答案,显示如何查看不同版本的文件有问题,例如,

# Common base version of the file.
git show :1:some_file.cpp

# 'Ours' version of the file.
git show :2:some_file.cpp

# 'Theirs' version of the file.
git show :3:some_file.cpp

我正在使用Microsoft的Visual Studio代码来解决冲突. 它很容易使用. 我在工作场所保持我的项目开放. 它检测和突出冲突. 此外,它提供了GUI选项,以选择我想要从头部或进入的任何变化。

此分類上一篇

请遵循以下步骤来解决 Git 中的合并冲突:

查看 Git 状态: git 状态 获取 patchset: git fetch (从您的 Git commit 查看正确的 patch) Checkout 一个本地分支(temp1 在我的例子中在这里): git checkout -b temp1 Pull 最近的内容从主: git pull -rebase 起源主 启动 mergetool 并检查冲突并纠正它们......并检查与您当前分支的远程分支中的变化: git mergetool Chec

使用耐心

对于一个大合并冲突,使用耐心为我提供了良好的结果,它会试图匹配区块而不是个别的线条。

例如,如果您更改您的程序的定位,默认 Git 合并策略有时匹配单手 { 属于不同的功能。

git merge -s recursive -X patience other-branch

从文档中:

With this option, merge-recursive spends a little extra time to avoid 
mismerges that sometimes occur due to unimportant matching lines 
(e.g., braces from distinct functions). Use this when the branches to 
be merged have diverged wildly.

与共同祖先相比

如果你有一个合并冲突,想要看到其他人在修改分支时想知道什么,有时更容易将他们的分支直接与普通祖先(而不是我们的分支)进行比较。

git diff $(git merge-base <our-branch> <their-branch>) <their-branch>

通常,您只希望看到特定文件的更改:

git diff $(git merge-base <our-branch> <their-branch>) <their-branch> <file>