解决Git合并冲突有利于他们在拉的变化

如何解决git合并冲突,以利于拉动的变化?

基本上我需要从工作树中删除所有冲突的变化,而不必通过一个git mergetool所有冲突,同时保持所有冲突的变化。 最好在拉动的同时进行,而不是之后。

 git pull -s recursive -X theirs <remoterepo or other repo> 

或者,简单地说,对于默认存储库:

 git pull -X theirs 

如果你已经处于冲突状态

 git checkout --theirs path/to/file 

你可以使用recursion“他们的”策略选项

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 . 

这是非常激烈的,所以确保你真的想在这样做之前把所有东西都擦掉。

好吧,照片我刚才的情景:

你尝试一个merge ,或者一个cherry-pick ,你停下来

 $ git cherry-pick 1023e24 error: could not apply 1023e24... [Commit Message] hint: after resolving the conflicts, mark the corrected paths hint: with 'git add <paths>' or 'git rm <paths>' hint: and commit the result with 'git commit' 

现在,你查看冲突的文件,你真的不想保留你的更改。 在我上面的例子中,这个文件在我的IDE自动添加的换行符上发生了冲突。 要撤销您的更改并接受他们的更改,最简单的方法是:

 git checkout --theirs path/to/the/conflicted_file.php git add path/to/the/conflicted_file.php 

这个(用你的版本覆盖传入的版本)是相反的

 git checkout --ours path/to/the/conflicted_file.php git add path/to/the/conflicted_file.php 

令人惊讶的是,我很难在网上find这个答案。

要解决与特定分支中版本的所有冲突:

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

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

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

对于默认存储库来说,这很简单:

 git pull -X theirs 

如果您已处于冲突状态, 则解决scheme稍有不同 ,但仍可pipe理。

请不要说,有时这不会工作

git结帐 – path/到/文件

要么

git checkout – 他们的path/到/文件

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

 git checkout HEAD -- path/to/file 

要么:

 git checkout MERGE_HEAD -- path/to/file 

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

 git add . 

如果你想了解更多,请看这里的托雷克精彩的post: git checkout –ours不会从未合并文件列表中删除文件

https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging

这基本上会做一个假合并。 它会logging一个新的合并提交作为父母的两个分支,但它甚至不会看你正在合并的分支。它只会logging合并的结果确切的代码在你当前的分支。

 $ git merge -s ours mundo 

通过“我们的”战略合并。

 $ git diff HEAD HEAD~ 

你可以看到我们所在的分支和合并的结果没有区别。

这基本上可以让Git认为一个分支在之后的合并时已经被合并了。 例如,假设你分支了一个发布分支,并且已经完成了一些工作,你会想在某个时候重新合并到你的主分支。 同时,master上的一些bug修复需要被反向移植到你的发行版本中。 您可以将bugfix分支合并到发布分支中,并将我们的同一分支合并到您的主分支中(即使修补程序已经存在),所以当您以后再次合并发行分支时,没有与修补程序发生冲突。

如果我希望主人能够反映新主题分支的变化,我发现这种情况非常有用。 我注意到,在某些情况下,他们不合并,没有冲突…例如

 $ git merge -Xtheirs topicFoo CONFLICT (modify/delete): js/search.js deleted in HEAD and modified in topicFoo. Version topicFoo of js/search.js left in tree. 

在这种情况下,我find的解决scheme是

 $ git checkout topicFoo 

从topicFoo,首先使用我们的策略合并在主,这将创build只是topicFoo状态的假提交。 $ git merge -s我们的主人

检查创build的合并提交

 $ git log 

现在结帐主分支

 $ git checkout master 

将主题分支合并回来,但是这次使用-X他们的recursion策略,现在将向您展示一个带有topicFoo状态的主分支。

 $ git merge -X theirs topicFoo