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


当前回答

使用耐心

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

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

其他回答

合并冲突发生时,同时对文件进行更改,这里是如何解决。

下面是简单的步骤,当你进入冲突状态时要做什么:

对于实用的教程,请检查: 剧本 5 - 解决混合冲突 由 Katacoda。

差异化

我成功地使用了DifMerge,它可以在Windows、macOS和Linux/Unix上视觉上比较和合并文件。

此分類上一篇

圖片來源:DifMerge(Linux屏幕拍攝)

git mergetool -t diffmerge .

马克思

brew install caskroom/cask/brew-cask
brew cask install diffmerge

而且,你可能(如果没有提供)需要下列额外简单的插槽放置在你的PATH(例如 /usr/bin):

#!/bin/sh
DIFFMERGE_PATH=/Applications/DiffMerge.app
DIFFMERGE_EXE=${DIFFMERGE_PATH}/Contents/MacOS/DiffMerge
exec ${DIFFMERGE_EXE} --nosplash "$@"

-Alt-Up/Down to jump to previous/Next changes. -Alt-Left/Right to accept change from left or right. -Alt-Up/Down to jump to previous/Next changes. -Alt-Left/Right to accept change from left or right

使用耐心

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

例如,如果您更改您的程序的定位,默认 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 fetch 然后 git merge 当运行 git fetch 然后 git rebase 当运行 git pull (实际上相当于上述条件之一) 当运行 git stash pop 当应用 git patches (以电子邮件传输的文件出口的命令)

你需要安装一个合并工具,它是兼容的Git解决冲突. 我个人使用KDiff3,我发现它很好和方便. 你可以下载它的Windows版本在这里:

https://sourceforge.net/项目/kdiff3/文件/

然后设置 Git 配置以使用 KDiff3 作为其 mergetool:

$ git config --global --add merge.tool kdiff3
$ git config --global --add mergetool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
$ git config --global --add mergetool.kdiff3.trustExitCode false

$ git config --global --add diff.guitool kdiff3
$ git config --global --add difftool.kdiff3.path "C:/Program Files/KDiff3/kdiff3.exe"
$ git config --global --add difftool.kdiff3.trustExitCode false

(请记住将路径替换为 KDiff3 EXE 文件的实际路径。

$ git mergetool

然后它打开Kdiff3,并首先试图自动解决合并冲突。

下面是Kdiff3看起来怎么样:

$ git mergetool
No files need merging

我要我的或他们的版本完整,或者想审查个别的变化,并决定每一个。

完全接受我的或他们的版本:

接受我的版本(本地,我们的):

git checkout --ours -- <filename>
git add <filename>              # Marks conflict as resolved
git commit -m "merged bla bla"  # An "empty" commit

接受其版本(远程,自己的):

git checkout --theirs -- <filename>
git add <filename>
git commit -m "merged bla bla"

如果你想做所有冲突文件运行:

git merge --strategy-option ours

git merge --strategy-option theirs

查看所有变更并单独接受它们

git mergetool 评论 改变并接受每个版本. git 添加 <filename> git commit -m "merged bla bla"

默认的 mergetool 在命令线上工作. 如何使用命令线 mergetool 应该是一个单独的问题。

您也可以为此安装视觉工具,例如通讯和运行

git mergetool -t meld

它将打开本地版本(ours),“base”或“合并”版本(合并的当前结果)和远程版本(天花板)。在完成时保存合并版本,运行 git mergetool -t 再次登记,直到您获得“不需要合并文件”,然后转到步骤3和4。

对于想要半手动解决合并冲突的 Emacs 用户:

git diff --name-status --diff-filter=U

显示所有需要冲突解决的文件。

打开这些文件中的每个一个,或所有一次,通过:

emacs $(git diff --name-only --diff-filter=U)

当您访问需要在 Emacs 中编辑的泡沫时,

ALT+x vc-resolve-conflicts

这将打开三个泡沫(矿,他们的,和输出泡沫)。导航按“n”(下一个区域),“p”(预测区域)。按“a”和“b”复制矿或他们的区域到输出泡沫,分别。

当完成时:按“q”。Emacs问你是否想保存这个泡沫:是的。

git add FILENAME

当所有类型的泡沫结束时

git commit

结束合并。