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


当前回答

如果你经常做小承诺,然后开始看看承诺评论与 git log --merge. 然后 git diff 会向你展示冲突。

我喜欢opendiff - Git 也支持 vimdiff, gvimdiff, kdiff3, tkdiff, meld, xxdiff,从盒子中出现,你可以安装其他: git config merge.tool “your.tool” 将设置您所选择的工具,然后 git mergetool 失败的合并后将向您展示背景中的 diffs。

每当您编辑文件以解决冲突时, git 添加文件名将更新索引,而您的 diff 将不再显示。

其他回答

我发现的最好的东西是使用“diff3”合并冲突风格:

<<<<<<<
Changes made on the branch that is being merged into. In most cases,
this is the branch that I have currently checked out (i.e. HEAD).
|||||||
The common ancestor version.
=======
Changes made on the branch that is being merged in. This is often a 
feature/topic branch.
>>>>>>>

如果冲突更长,那么我会把三个部分切成三个单独的文件,如“小”,“普通”和“天花板”。

diff common mine
diff common theirs

提示2

git log --merge -p <name of file>

如果你有自动测试,运行这些,如果你有绳子,运行它,如果这是一个可构建的项目,然后在你承诺之前建造它,等等,在所有情况下,你需要做一些测试,以确保你的变化没有打破任何东西。

例如,如果你知道你和另一个人都在不同的重复工作,这两者都会影响相同的文件集,你应该提前彼此谈话,并更好地了解你每个人都在做什么类型的变化。

如果你不确定合并,不要强迫它。

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

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

识别哪些文件处于冲突中(Git应该告诉你这一点)。 打开每个文件并检查Difs; Git demarcates它们. 希望它将是显而易见的哪个版本的每个区块保持。 你可能需要讨论它与同开发人员谁承诺的代码. 一旦你解决了冲突在一个文件 git 添加到_file. 一旦你解决了所有冲突,做 git rebase --continue 或 whateve

使用耐心

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

例如,如果您更改您的程序的定位,默认 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>

我明白什么是合并冲突,但当我看到Git diff的输出时,这对我一开始就看起来很荒谬:

git diff
++<<<<<<< HEAD
 + display full last name boolean in star table
++=======
+ users viewer.id/star.id, and conversation uses user.id
+
++>>>>>>> feat/rspec-tests-for-cancancan

但是,这里是帮助我的一点:

<<<<<和 =======之间的一切都是在一个文件中的一切,而在 =======和 >>>>>之间的一切都是在另一个文件中的一切,所以你必须做的就是打开与合并冲突的文件,并从任何分支中删除这些线条(或者只是让它们相同),合并将立即成功。