简体   繁体   English

总是在冲突中使用合并分支的版本

[英]Always use version from merged branch on conflicts

Currently I am working in a feature branch that will soon be merged back into master.目前我正在一个功能分支中工作,该分支很快将合并回master。 I recently tried just that and had some merge conflicts, which I had to fix manually.我最近尝试过,但遇到了一些合并冲突,我必须手动修复。

So, is it possible to tell git to always use the version from the merged branch in order to avoid merge conflicts beforehand?那么,是否可以提前告诉 git 始终使用合并分支中的版本以避免合并冲突? In this case I fixed the conflicts manually but always chose the version from the merged branch, so this would save me some tedious work.在这种情况下,我手动修复了冲突,但总是从合并的分支中选择版本,这样可以节省一些繁琐的工作。

You can do exactly this in git with the following command, assuming that feature is the name of your feature branch:您可以使用以下命令在 git 中执行此操作,假设该feature是您的功能分支的名称:

git merge -s recursive -X theirs feature

This says to use the "recursive" merge strategy, but with the "theirs" option.这表示使用“递归”合并策略,但使用“他们的”选项。 This means that when there is a conflict, it will be automatically resolved by taking the version of the hunk from the feature branch, not your current branch.这意味着当发生冲突时,它将通过从功能分支而不是您当前的分支中获取大块的版本来自动解决。 (Note that this is completely different from the "theirs" merge strategy, which has now been removed from git.) (请注意,这与“他们的”合并策略完全不同,后者现已从 git 中删除。)

This feature was introduced in git v1.7.0.此功能在 git v1.7.0 中引入。

You can't use the recursive "ours" strategy.您不能使用递归的“我们的”策略。 It would omit your changes that didn't conflict.它会省略您没有冲突的更改。

You could script getting the file names of the conflicted files, and do a git checkout --ours -- filename followed by a git add filename.您可以编写脚本获取冲突文件的文件名,然后执行 git checkout --ours -- filename 后跟 git 添加文件名。

If you're getting the same conflicts over and over, turn on rerere and that may be enough so you don't have to resolve the conflicts.如果您一遍又一遍地遇到相同的冲突,请打开 rerere,这可能就足够了,因此您不必解决冲突。

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM