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

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


当前回答

接受远程更改(他们的),如果发生冲突,您将收到以下错误:

fatal: Not possible to fast-forward, aborting.

因此,您可能希望快速地拉动并接受他们的更改:

$ git pull -X theirs --ff

其他回答

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

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

$ 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 fetch && git reset --hard origin/master

git pull -X theirs

git pull origin master

git pull-X他们的答案可能会创建一个难看的合并提交,或者发出

错误:您对以下文件的本地更改将被合并覆盖:

如果您想简单地忽略对repo中文件的任何本地修改,例如,在始终应为源镜像的客户端上,请运行以下命令(用所需的分支替换master):

git fetch && git reset --hard origin/master

它是如何工作的?git fetch执行git pull,但不合并。然后gitreset-hard使您的工作树与上一次提交相匹配。您对回购中文件的所有本地更改都将被丢弃,但新的本地文件将被保留。

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

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

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

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

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

VS代码(集成Git)IDE用户:

如果要接受冲突文件中的所有传入更改,请执行以下步骤。

1. Go to command palette - Ctrl + Shift + P
2. Select the option - Merge Conflict: Accept All Incoming

类似地,您可以使用其他选项,如“全部接受”、“全部接受当前”等。,