强制 Git 在合并期间总是选择较新的版本?

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

Force Git to always choose the newer version during a merge?

gitmergeconflict

提问by bartek

Let's assume I mergegit and there is a merge conflict.

让我们假设我mergegit 并且存在合并冲突。

My question is: how can I force git to always choose the newer version of code in conflict so I won't need to resolve the conflict by hand?

我的问题是:如何强制 git 总是在冲突中选择较新版本的代码,这样我就不需要手动解决冲突?

回答by Renato Zannon

It is not exactly the "newer" version, but you can tell git to always prefer the version on the current branch using git merge branch -X ours, or to prefer the version of the branch being merged, using git merge branch -X theirs.

它不完全是“较新”版本,但您可以告诉 git 始终git merge branch -X ours使用git merge branch -X theirs.

From man git-merge:

来自man git-merge

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. For a binary file, the entire contents are taken from our side.

theirs:

This is the opposite of "ours".

我们的:

此选项通过支持我们的版本强制自动解决冲突的大块头。来自另一棵树的与我们这边不冲突的变化会反映到合并结果中。对于二进制文件,所有内容都取自我们这边。

他们的:

这与“我们的”相反。

回答by wolfgang

I use this,

我用这个,

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