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.
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 attributesfor more information.tell git that you want to use
mymergewhen merging any file namedfoo.txtin the repository:make some test data on three branches:
merge the branches (note no conflicts!):
profit!