如何解决git合并冲突以支持拉取的更改?

我希望从工作树中删除所有冲突的更改,而不必使用gitmergetool处理所有冲突,同时保持所有无冲突的更改。最好是,我想在拉的时候做,而不是事后。


当前回答

您可以使用递归的“他们的”策略选项:

git merge --strategy-option theirs

来自男子:

ours
    This option forces conflicting hunks to be auto-resolved cleanly by 
    favoring our version. Changes from the other tree that do not 
    conflict with our side are reflected to the merge result.

    This should not be confused with the ours merge strategy, which does 
    not even look at what the other tree contains at all. It discards 
    everything the other tree did, declaring our history contains all that
    happened in it.

theirs
    This is opposite of ours.

注意:正如手册页所说,“我们”的合并策略选项与“我们”合并策略非常不同。

其他回答

如果你已经处于冲突状态,并且你想接受他们的一切:

git checkout --theirs .
git add .

如果你想做相反的事情:

git checkout --ours .
git add .

这是非常激烈的,所以在做这件事之前,一定要确保你真的想把所有的东西都这样抹掉。

git checkout——我们的/他们的并不专门解决冲突。它从我们/他们的文件中检出(取出整个文件)。

假设我们有一个文件foo,其中包含两个committes/branches/trees/whatever中的更改。如果存在由他们引入的冲突以及修改,并且我们希望使用我们的(然后使用签出)来解决冲突,我们的foo将丢弃引入冲突的更改以及修改。

使用SED

使用他们的解决方案:

sed-i-e'/^''/^=====//,/^========/d'-e'/^>>>>>>/d'foo

-i就地修改文件,/^^^===========</,/^=====/d删除(我们的)和(包括)之间的所有内容/^>>>>>>>/d删除剩余的冲突标记-e为SED指定多个模式foo文件

使用我们的解决方案:

sed-i-e'/^'>''''</d'-e'/^=====/,/^>>>>>>/d'foo

我编写了一个脚本,您可以调用gitresolve-o/-t/-b。

创建自定义合并工具

您可以创建自定义合并工具。在上述sed脚本的基础上,您可以在git配置中添加如下内容:

[mergetool "ours"]
    cmd = "sed -i -e '/^<<<<<<</d' -e '/^=======/,/^>>>>>>>/d' -- $MERGED"

并将其称为gitmergetool--tool=ours

我有一个长期运行的下一个版本分支,对开发时更改的文件、在两个分支的不同位置添加的文件等进行了大量删除。

我想将下一个版本分支的全部内容进行开发,所有内容都在一次巨大的合并提交中。

对我有效的上述命令组合是:

git merge -X theirs next-version
# lots of files left that were modified on develop but deleted on next-version
git checkout next-version .
# files removed, now add the deletions to the commit
git add .
# still have files that were added on develop; in my case they are all in web/
git rm -r web

这不是一个新的答案,只是结合了许多答案中的一部分,部分是为了保证你可能需要所有这些答案。

要解决与特定分支中版本的所有冲突,请执行以下操作:

git diff --name-only --diff-filter=U | xargs git checkout ${branchName}

因此,如果您已经处于合并状态,并且希望保留冲突文件的主版本:

git diff --name-only --diff-filter=U | xargs git checkout master

请注意,有时这是行不通的:

git checkout--我们的路径/to/file

or

git checkout--他们的路径/to/file

我改为这样做,假设HEAD是我们的,MERGE_HEAD是他们的

git checkout HEAD -- path/to/file

or:

git checkout MERGE_HEAD -- path/to/file

在我们这样做之后,我们很好:

git add .

如果你想了解更多,请点击此处查看精彩的torek帖子:git checkout--我们不会从未合并文件列表中删除文件