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


当前回答

简单地说,如果你知道其中一个存储库的变化并不重要,并且想解决所有变化,有利于另一个,请使用:

git checkout . --ours

以您的存储库的利益来解决更改,或者

git checkout . --theirs

以解决其他或主要存储库的利益的变更。

或者你将不得不使用一个 GUI 合并工具,一步一步通过文件,说合并工具是 p4merge,或者写下你已经安装的任何一个的名字。

git mergetool -t p4merge

完成文件后,您将不得不保存和关闭,以便下一个将打开。

其他回答

使用耐心

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

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

From ssh://gitosis@example.com:22/projectname
 * branch            master     -> FETCH_HEAD
Updating a030c3a..ee25213
error: Entry 'filename.c' not uptodate. Cannot merge.

所以你更新并尝试一次,但有冲突:

git add filename.c
git commit -m "made some wild and crazy changes"
git pull origin master

From ssh://gitosis@example.com:22/projectname
 * branch            master     -> FETCH_HEAD
Auto-merging filename.c
CONFLICT (content): Merge conflict in filename.c
Automatic merge failed; fix conflicts and then commit the result.

所以你决定看看这些变化:

git mergetool

哦,我的,哦,我的,上流改变了一些事情,但只是用我的变化......不......他们的变化......

git checkout --ours filename.c
git checkout --theirs filename.c
git add filename.c
git commit -m "using theirs"

然后,我们试着最后一次。

git pull origin master

From ssh://gitosis@example.com:22/projectname
 * branch            master     -> FETCH_HEAD
Already up-to-date.

是的!

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

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

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

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

我跟随下面的过程。

解决合并冲突的过程:

首先,把最新的从目的地分支到你想合并 git pull 起源开发 随着你得到最新的从目的地,现在解决冲突手动在一个 IDE 通过删除这些额外的字符。

这就是它,你会看到它在你的拖请求中解决,如果你使用Bitbucket或GitHub。