在拉取期间解决 Git 合并冲突以支持其更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10697463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:44:30  来源:igfitidea点击:

Resolve Git merge conflicts in favor of their changes during a pull

gitgit-mergegit-merge-conflict

提问by sanmai

How do I resolve a git merge conflict in favor of pulled changes?

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

Basically I need to remove all conflicting changes from a working tree without having to go through all of the conflicts with a git mergetoolwhile keeping all conflict-free changes. Preferably doing this while pulling, not afterwards.

基本上我需要从工作树中删除所有冲突的更改,而不必经历所有冲突,git mergetool同时保持所有无冲突的更改。最好在拉动时这样做,而不是之后。

采纳答案by Pascal Fares

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

Or, simply, for the default repository:

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

git pull -X theirs

If you're already in conflicted state...

如果您已经处于冲突状态...

git checkout --theirs path/to/file

回答by Ikke

You can use the recursive "theirs" strategy option:

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

git merge --strategy-option theirs

git merge --strategy-option theirs

From the man:

来自男人

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.

Note: as the man page says, the "ours" merge strategy optionis very different from the "ours" merge strategy.

注:由于该男子页称,“我们的”合并的策略选项是从“我们”合并完全不同的策略

回答by lots-to-learn

If you're already in conflicted state, and you want to just accept allof theirs:

如果您已经处于冲突状态,并且只想接受他们的所有状态:

git checkout --theirs .
git add .

If you want to do the opposite:

如果你想做相反的事情:

git checkout --ours .
git add .

This is pretty drastic, so make sure you really want to wipe everything out like this before doing it.

这是非常激烈的,所以在做之前确保你真的想像这样清除所有东西。

回答by Theodore R. Smith

OK so, picture the scenario I was just in:

好吧,想象一下我刚刚所处的场景:

You attempt a merge, or maybe a cherry-pick, and you're stopped with

您尝试 a merge,或者可能是 a 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'

Now, you view the conflicted file and you really don't want to keep your changes. In my case above, the file was conflicted on just a newline my IDE had auto-added. To undo your changes and accept their's, the easiest way is:

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

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

The converse of this (to overwrite the incoming version with your version) is

与此相反(用您的版本覆盖传入版本)是

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

Surprisingly, I couldn't find this answer very easily on the Net.

令人惊讶的是,我无法在网上很容易地找到这个答案。

回答by Dan Dascalescu

The git pull -X theirsanswers may create an ugly merge commit, or issue an

git pull -X theirs答案可能创建一个丑陋的合并提交,或发出

error: Your local changes to the following files would be overwritten by merge:

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

If you want to simply ignore any local modifications to files from the repo, for example on a client that should always be a mirror of an origin, run this (replace masterwith the branch you want):

如果您只想忽略对 repo 中文件的任何本地修改,例如在应该始终是源镜像的客户端上,请运行此命令(替换master为您想要的分支):

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

How does it work? git fetchdoes git pullbut without merge. Then git reset --hardmakes your working tree match the last commit. All of your local changes to files in the repo will be discarded, but new local files will be left alone.

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

回答by Mohit Kanojia

After git merge, if you get conflicts and you want either your or their

在 git merge 之后,如果您遇到冲突并且想要您的或他们的

git checkout --theirs .
git checkout --ours .

回答by Ariel Alvarez

To resolve all conflicts with the version in a particular branch:

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

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

So, if you are already in the merging state, and you want to keep the master version of the conflicting files:

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

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

回答by Nicolas D

Please not that sometimes this will not work:

请注意,有时这不起作用

git checkout --ours path/to/file

git checkout --ours path/to/file

or

或者

git checkout --theirs path/to/file

git checkout --theirs path/to/file

I did this instead, assuming HEAD is oursand MERGE_HEAD is theirs

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

git checkout HEAD -- path/to/file

or:

或者:

git checkout MERGE_HEAD -- path/to/file

After we do this and we are good:

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

git add .

If you want to understand more, see wonderful post of torek here : git checkout --ours does not remove files from unmerged files list

如果您想了解更多信息,请在此处查看 torek 的精彩帖子: git checkout --ours 不会从未合并的文件列表中删除文件

回答by SridharKritha

VS Code (integrated Git) IDE Users:

VS Code(集成 Git)IDE 用户:

If you want to accept all the incoming changes in the conflict filethen do the following steps.

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

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

Similarly you can do for other options like Accept All Both, Accept All Current etc.,

同样,您可以对其他选项进行操作,例如“接受所有两者”、“接受所有当前”等,

回答by Gordon

I had a long-running next-versionbranch with tons of deletions to files that had changed on develop, files that had been added in different places on both branches, etc.

我有一个长期运行的next-version分支,其中删除了大量已更改的develop文件、已添加到两个分支上不同位置的文件等。

I wanted to take the entire contents of the next-versionbranch into develop, all in one whopping merge commit.

我想将next-version分支的全部内容develop全部放入一个巨大的合并提交中。

The combination of the above commands that worked for me was:

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

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

Not a new answer, just combining bits from many answers, partly to reassure that you might need allof these answers.

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