git 如何在合并期间更喜欢来自一个分支的文件?

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

How to prefer files from one branch during a merge?

git

提问by Bastian

Some time ago I created a branch from my masterbranch. Let's call it new_feature. While I was working on new_feature, the masterbranch followed its normal evolution. Now that new_featureis ready to be merged into masterI see some conflicts, all of them are into files that are totally unrelated to the actual new feature (like some config files and the likes that have changed on master). I'm going to solve the conflicts manually but I was wondering, since it's a situation that happens quite often, how I could just merge the new_featurebranch into masterwhile always keeping the masterversion of the files in case of conflict?

前段时间我从我的master分支创建了一个分支。让我们称之为new_feature。在我工作时new_featuremaster分支遵循其正常发展。现在new_feature已准备好合并到master我看到一些冲突中,所有这些冲突都包含在与实际新功能完全无关的文件中(例如某些配置文件和已更改的类似文件master)。我将手动解决冲突,但我想知道,因为这种情况经常发生,我如何才能将new_feature分支合并,master同时始终保留master文件版本以防发生冲突?

I'm sure it's easy and is related to something like 'keep version' but since it's a pretty sensitive subject I'd rather ask than be sorry.

我确定这很容易,并且与“保留版本”之类的内容有关,但由于这是一个非常敏感的主题,因此我宁愿问也不愿道歉。

回答by Schleis

As mention in the comment there are several strategies in the documentation. You can also find them here: http://git-scm.com/docs/git-merge

正如评论中提到的,文档中有几种策略。你也可以在这里找到它们:http: //git-scm.com/docs/git-merge

You are looking for either git merge -s recursive -X oursor git merge -s recursive -X theirsdepending on the branch that you are on. Be careful with these as you can accidentally miss changes to your files from the other branch and these would be overwritten.

您正在寻找git merge -s recursive -X oursgit merge -s recursive -X theirs取决于您所在的分支。小心这些,因为您可能会不小心错过其他分支对文件的更改,这些更改将被覆盖。

Another method, which I prefer due to more control, is to git checkout <other-branch> -- <list of files>. This way I don't accidentally overwrite a file with the wrong changes.

由于更多的控制,我更喜欢另一种方法是git checkout <other-branch> -- <list of files>. 这样我就不会意外地用错误的更改覆盖文件。