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

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


当前回答

如果您已经处于冲突状态,并且不想逐个签出路径。你可以试试

git merge --abort
git pull -X theirs

其他回答

2023年更新!

如果您想接受所有当前更改并忽略任何传入的更改,可以执行以下操作:

git merge [branch] --strategy-option ours

[branch]应替换为要合并到当前分支中的分支的名称。

相反,如果您知道要覆盖任何当前更改并接受来自传入更改的所有冲突,则可以使用他们的策略:

git merge [branch] --strategy-option theirs

如果您已经处于冲突状态,并且不想逐个签出路径。你可以试试

git merge --abort
git pull -X theirs

好吧,想象一下我刚才的情景:

你尝试合并,或是樱桃采摘,但你被阻止了

$ 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

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

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

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

git pull -X theirs

如果你已经处于冲突状态。。。

git checkout --theirs path/to/file

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

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.

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