如何通过获取当前分支中的版本来自动解决 Git 冲突?

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

How to automatically resolve a Git conflict by taking the version in the current branch?

gitmergeautomationconflict

提问by pts

Let's suppose that I get a merge conflict on foo/bar.txtwhen running this:

假设我foo/bar.txt在运行时遇到合并冲突:

$ git checkout A
$ git merge B

I'd like to automatically resolve the conflict by taking foo/bar.txtfrom branch A. (I know what I'm doing, and I do need this. The version in branch B is wrong, and I don't care about losing changes in the working tree in this case.) It seems that I can do it by running these commands:

我想通过foo/bar.txt从分支 A 中获取来自动解决冲突。(我知道我在做什么,我确实需要这个。分支 B 中的版本是错误的,我不在乎丢失工作中的更改树在这种情况下。)似乎我可以通过运行这些命令来做到这一点:

$ git reset    foo/bar.txt
$ git checkout foo/bar.txt

Is there a simpler, single-command solution?

有没有更简单的单命令解决方案?

Unfortunately these commands change foo/bar.txteven if there is no conflict, and I don't want that. If there is no conflict, I want want to keep foo/bar.txtin whatever state git merge Bhas left it.

不幸的是,foo/bar.txt即使没有冲突,这些命令也会改变,我不希望那样。如果没有冲突,我想保持foo/bar.txtgit merge B离开它的任何状态。

So I need a Unix shell command, which would detect if there is a conflict in foo/bar.txt, and if there is, it would resolve the conflict by taking the version of foo/bar.txtfrom the current branch. It wouldn't do anything else, i.e. it wouldn't modify other files, it wouldn't commit the changes, and it wouldn't change foo/bar.txtif there is no conflict in that file.

所以我需要一个Unix shell命令,如果在一个冲突,将检测foo/bar.txt,如果有,它会采取的版本解决冲突foo/bar.txt从当前分支。它不会做任何其他事情,即它不会修改其他文件,它不会提交更改,foo/bar.txt如果该文件中没有冲突,它也不会更改。

采纳答案by jthill

If you want to do it as a one-off, the single-line command is:

如果你想一次性完成,单行命令是:

$ git checkout --ours foo/bar.txt  # <-- resets it only if the merge found conflicts in this file
$ git checkout HEAD -- foo/bar.txt # <-- resets it to that specific version no matter what

To configure git's merge to permanently ignore all upstream changes to a locally-changed file:

要将 git 的合并配置为永久忽略对本地更改文件的所有上游更改:

$ git config merge.pin.driver true
$ echo foo/bar.txt merge=pin >> .git/info/attributes

(trueabove is just the unix truecommand, its success says it made the local version look right, in this case by doing nothing to it. You can of course get more sophisticated with your merge commands.)

true上面只是 unixtrue命令,它的成功表明它使本地版本看起来正确,在这种情况下,它什么都不做。当然,您可以使用合并命令变得更复杂。)

I think you don't want merge --strategy=oursor --strategy-option=ours, those apply to entire merges.

我认为你不想要merge --strategy=oursor --strategy-option=ours,那些适用于整个合并。

回答by knittl

You can specify the merge strategy option oursto the recursive(the default) merge strategy. It will do a normal merge, but in case of conflicting hunks will choose the version of the current branch.

您可以oursrecursive(默认)合并策略指定合并策略选项。它会做一个正常的合并,但在冲突的情况下,帅哥会选择当前分支的版本。

git checkout A
git merge -Xours B