git 如何使用 HEAD 和任何合并工具解决所有冲突
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20229222/
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 resolve ALL conflicts using HEAD, with any mergetool
提问by leonsas
So for some reason I'm getting a lot of conflicts with a new merged hotfix. The file that was actually [manually] changed has no conflict. All the conflicts are in files that were untouched during the fix and apparently its an issue with whitespaces. I'll try to figure that problem later but now I need to merge the hotfix and deploy.
因此,出于某种原因,我与新合并的修补程序发生了很多冲突。实际[手动]更改的文件没有冲突。所有冲突都在修复期间未受影响的文件中,显然是空格问题。我稍后会尝试解决这个问题,但现在我需要合并修补程序和部署。
How can I resolve ALL conflicts to use the HEAD version? I don't want to go file by file. Yes, I know its a bad practice but the conflicts are all whitespaces and I know HEAD is correct — passing all tests and running fine in production.
如何解决所有冲突以使用 HEAD 版本?我不想一个文件一个文件地走。是的,我知道这是一个不好的做法,但冲突都是空格,我知道 HEAD 是正确的——通过所有测试并在生产中运行良好。
Any ideas?
有任何想法吗?
I'm using OSX.
我正在使用 OSX。
回答by NHDaly
git merge -Xours origin/master
will do a merge with origin/master
(the same thing that git pull origin master
does) and will resolve any conflicts by taking the versions from your local branch.
将与origin/master
(做同样的事情git pull origin master
)进行合并,并将通过从您的本地分支获取版本来解决任何冲突。
If you're already part-way through the bad merge, you can reset everything to the head first with git reset --hard HEAD
.
如果您已经完成了错误合并,您可以先使用git reset --hard HEAD
.
In that case, you should do
在这种情况下,你应该做
git reset --hard HEAD
git merge -Xours origin/master
And that should fix your problem!
这应该可以解决您的问题!
(also worth mentioning, -Xtheirs
will do the same thing, but take the upstream version in any conflicts.)
(也值得一提,-Xtheirs
会做同样的事情,但在任何冲突中都使用上游版本。)
Also, most likely the conflicts are because the upstream version is using windows-style line endings, and whatever program you edited the files in on your local machine is using mac-style or linux-style line endings.
此外,最有可能的冲突是因为上游版本使用的是 windows 风格的行尾,而你在本地机器上编辑文件的任何程序都使用了 mac 风格或 linux 风格的行尾。
There are options you can set in git to always commit windows-style or linux-style line endings, but always checkout mac-style or linux-style in your working directory.
您可以在 git 中设置一些选项以始终提交 windows 样式或 linux 样式的行尾,但始终在您的工作目录中签出 mac 样式或 linux 样式。
See this link for more info: https://help.github.com/articles/dealing-with-line-endings
有关更多信息,请参阅此链接:https: //help.github.com/articles/dealing-with-line-endings
回答by Yametekudasai
If you have conflict on your local branch you can simply run these commands:
如果你的本地分支有冲突,你可以简单地运行这些命令:
git checkout --conflict=merge .
git checkout --ours .
To solve conflicts using your local branch.
使用本地分支解决冲突。
回答by Bert F
I would:
我会:
$ git checkout master # or where ever you want to merge the hotfix into
$ git merge --no-commit -Xours <hotfix-branch>
$ git status # make sure the only change is the file you wanted
$ git diff # make sure they changes are what you wanted
$ git commit -m "<your merge message"
This will use the default recursive strategy pull in any non-conflicting files, but it will resolve any conflicting files by using simply using the master/HEAD version. The docs:
这将在任何非冲突文件中使用默认递归策略拉取,但它会通过简单地使用 master/HEAD 版本来解决任何冲突文件。文档:
$ git merge --help
....
recursive
... This is the default merge strategy when pulling or merging one branch.
The recursive strategy can take the following options:
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. ....