如何通过获取当前分支中的版本来自动解决 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
How to automatically resolve a Git conflict by taking the version in the current branch?
提问by pts
Let's suppose that I get a merge conflict on foo/bar.txt
when running this:
假设我foo/bar.txt
在运行时遇到合并冲突:
$ git checkout A
$ git merge B
I'd like to automatically resolve the conflict by taking foo/bar.txt
from 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.txt
even if there is no conflict, and I don't want that. If there is no conflict, I want want to keep foo/bar.txt
in whatever state git merge B
has left it.
不幸的是,foo/bar.txt
即使没有冲突,这些命令也会改变,我不希望那样。如果没有冲突,我想保持foo/bar.txt
在git 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.txt
from 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.txt
if 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
(true
above is just the unix true
command, 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=ours
or --strategy-option=ours
, those apply to entire merges.
我认为你不想要merge --strategy=ours
or --strategy-option=ours
,那些适用于整个合并。
回答by knittl
You can specify the merge strategy option ours
to 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.
您可以ours
为recursive
(默认)合并策略指定合并策略选项。它会做一个正常的合并,但在冲突的情况下,帅哥会选择当前分支的版本。
git checkout A
git merge -Xours B