Git 使用 --ours/--theirs 解决所有文件的冲突

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24743769/
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 18:17:58  来源:igfitidea点击:

Git resolve conflict using --ours/--theirs for all files

gitconflictresolvemerge-conflict-resolution

提问by exe163

Is there a way to resolve conflict for all files using checkout --oursand --theirs? I know that you can do it for individual files but couldn't find a way to do it for all.

有没有办法使用 checkout--ours和解决所有文件的冲突--theirs?我知道您可以为单个文件执行此操作,但无法找到为所有文件执行此操作的方法。

回答by Dmitri

Just grep through the working directory and send the output through the xargs command:

只需通过工作目录 grep 并通过 xargs 命令发送输出:

grep -lr '<<<<<<<' . | xargs git checkout --ours

or

或者

grep -lr '<<<<<<<' . | xargs git checkout --theirs

How this works: grepwill search through every file in the current directory (the .) and subdirectories recursively (the -rflag) looking for conflict markers (the string '<<<<<<<')

这是如何工作的:grep.递归搜索当前目录(the )和子目录(-r标志)中的每个文件,寻找冲突标记(字符串'<<<<<<<')

the -lor --files-with-matchesflag causes grep to output only the filename where the string was found. Scanning stops after first match, so each matched file is only output once.

-l--files-with-matches标志使grep的输出只有在string中找到的文件名。第一次匹配后扫描停止,因此每个匹配的文件只输出一次。

The matched file names are then pipedto xargs, a utility that breaks up the piped input stream into individual arguments for git checkout --oursor --theirs

然后将匹配的文件名通过管道传输xargs,这是一个实用程序,可将管道输入流分解为用于git checkout --ours--theirs

More at this link.

更多信息请访问此链接

Since it would be very inconvenient to have to type this every time at the command line, if you do find yourself using it a lot, it might not be a bad idea to create an aliasfor your shell of choice: Bashis the usual one.

由于每次都必须在命令行输入这个非常不方便,如果你发现自己经常使用它,为你选择的 shell创建一个别名可能不是一个坏主意:Bash是常用的.

This method should work through at least Git versions 2.4.x

此方法至少应适用于 Git 版本2.4.x

回答by ThanksForAllTheFish

You can -Xoursor -Xtheirswith git mergeas well. So:

你也可以-Xours-Xtheirswith git merge。所以:

  1. abort the current merge (for instance with git reset --hard HEAD)
  2. merge using the strategy you prefer (git merge -Xoursor git merge -Xtheirs)
  1. 中止当前合并(例如 with git reset --hard HEAD
  2. 使用您喜欢的策略合并(git merge -Xoursgit merge -Xtheirs

DISCLAIMER: of course you can choose only one option, either -Xoursor -Xtheirs, do use different strategy you should of course go file by file.

免责声明:当然,您只能选择一个选项,-Xours或者-Xtheirs,使用不同的策略,您当然应该逐个文件地进行。

I do not know if there is a way for checkout, but I do not honestly think it is terribly useful: selecting the strategy with the checkout command is useful if you want different solutions for different files, otherwise just go for the merge strategy approach.

我不知道是否有办法checkout,但老实说,我不认为它非常有用:如果您想要不同文件的不同解决方案,则使用 checkout 命令选择策略很有用,否则就选择合并策略方法。

回答by Cory Petosky

git checkout --[ours/theirs] .will do what you want, as long as you're at the root of all conflicts. ours/theirs only affects unmerged files so you shouldn't have to grep/find/etc conflicts specifically.

git checkout --[ours/theirs] .只要您是所有冲突的根源,就会做您想做的事。我们的/他们的仅影响未合并的文件,因此您不必专门 grep/find/etc 冲突。

回答by Peter

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

Seems to do the job. Note that you have to be cd'ed to the root directory of the git repo to achieve this.

似乎可以完成这项工作。请注意,您必须被 cd 到 git repo 的根目录才能实现此目的。

回答by Matt Browne

In case anyone else is looking to simply overwrite everything from one branch (say master) with the contents of another, there's an easier way:

如果其他人希望用另一个分支的内容简单地覆盖一个分支(比如 master)的所有内容,有一种更简单的方法:

git merge master --strategy=ours

Thanks to https://stackoverflow.com/a/1295232/560114

感谢https://stackoverflow.com/a/1295232/560114

Or for the other way around, see Is there a "theirs" version of "git merge -s ours"?

或者反过来,请参阅是否有“他们的”版本的“git merge -s ours”?

回答by iWheelBuy

function gitcheckoutall() {
    git diff --name-only --diff-filter=U | sed 's/^/"/;s/$/"/' | xargs git checkout --
}

I've added this function in .zshrcfile.

我在.zshrc文件中添加了这个函数。

Use them this way: gitcheckoutall theirsor gitcheckoutall ours

以这种方式使用它们: gitcheckoutall theirsgitcheckoutall ours