合并新代码的冲突解决方案
假设我有一些已订购的代码,但订购不是技术要求。
apple
kiwi
strawberry
然后我有两个要合并的主题,其差异如下所示:
TOPIC BRANCH: orange
kiwi
+ orange
strawberry
还有是否
TOPIC BRANCH: pear
kiwi
+ pear
strawberry
有办法让这两个补丁自动解决?在我看来,这是一个合并冲突,因为它们竞争同一条新线。我提出的一个解决方案是重新排序其中一个更改,因为排序顺序只是一个软要求(其中水果实际上是函数定义)。
TOPIC BRANCH: pear'
apple
+ pear
kiwi
现在我们可以将 orange
和 pear'
合并在一起形成:
_ apple
p pear
_ kiwi
o orange
_ strawberry
是否有其他方法可以解决这个问题,以便保持顺序?我还认为 pear
必须位于 orange
的下游,这样 orange
始终获得优先级,并且不会再出现合并冲突。但这是错误的依赖关系,因为 orange
和 pear
是两个独立的功能分支。
一个可以在另一个之前被主干化,但这并不能解决集成分支的问题。
编辑:我突然意识到可以保留两个大块头(我猜只是添加?)可能有两种合并策略,称为“我第一”和“你第一”,这样可以在两个之间以非交互方式解决不明确的排序分支机构。
Let's say I have some code that is ordered, but the ordering is not a technical requirement.
apple
kiwi
strawberry
And then I have two topics that I want to merge in, whose diffs look like:
TOPIC BRANCH: orange
kiwi
+ orange
strawberry
And also
TOPIC BRANCH: pear
kiwi
+ pear
strawberry
Is there a way for these two patches to get resolved automatically? It seems to me like it is a merge conflict since they compete for the same new line. A solution I've come up with is to reorder one of the changes since the sort order is only a soft requirement (where fruits are actually function definitions).
TOPIC BRANCH: pear'
apple
+ pear
kiwi
So now we can merge orange
and pear'
together to form:
_ apple
p pear
_ kiwi
o orange
_ strawberry
Are there other ways to resolve this such that the ordering can be kept? I also thought of pear
having to downstream from orange
such that orange
always gets priority and there wouldn't be a merge conflict anymore. But this is a false dependency since orange
and pear
are two separate feature branches.
One could be mainlined into the trunk before the other but that doesn't address integration branches.
Edit: It just dawned at me for two hunks that could be kept (additions only I guess?) there could be two merge strategies called "me first" and "you first" such that an ambiguous ordering could be resolved non-interactively between two branches.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本方法是定义一个自定义合并工具,然后使用 git 属性功能告诉 git 对这些文件使用该自定义合并工具。
示例:
创建测试存储库:
<前><代码>$ git init t
$ cd t
定义一个名为
mymerge
的自定义合并工具:上面的合并工具连接文件,对结果行进行排序,然后删除重复的行。如果您不想更改顺序,请将上面的命令替换为可以执行您想要的操作的自定义脚本。请参阅
git help attribute
了解更多信息。告诉 git 您在合并存储库中任何名为
foo.txt
的文件时要使用mymerge
:make some test data on三个分支:
合并分支(注意没有冲突!):
利润!
The basic approach is to define a custom merge tool and then use the git attributes feature to tell git to use that custom merge tool for those files.
Example:
create a test repository:
define a custom merge tool called
mymerge
:The above merge tool concatenates the files, sorts the resulting lines, and then removes duplicate lines. If you don't want to change the order, replace the above command with a custom script that does what you want. See
git help attributes
for more information.tell git that you want to use
mymerge
when merging any file namedfoo.txt
in the repository:make some test data on three branches:
merge the branches (note no conflicts!):
profit!
如果需要订购的代码位于单独的文件中,您可以使用自定义合并工具,该工具接受两个输入文件,读取它们并输出其内容,然后排序到结果文件中。然后,您将告诉 git 使用该文件的自定义合并工具。我对 git 的了解还不够,不知道您是否可以根据文件类型或某种正则表达式设置自定义合并工具,但这个想法可能会给您带来您想要的东西。
If the code that needs to be ordered is in a separate file, you could use a custom merge tool that takes two input files, reads them and outputs their contents, sorted into your result file. You would then tell git to use the custom merge tool for this file. I don't know enough about git to know if you can set custom merge tools based on file type or regex of some sort though, but it is an idea that might give you what you want.