Git: Keep duplicates during merge union

Viewed 31

I have file where lines supposed to be only added, not changed. Therefore I used merge union strategy. But it doesn't work good for this case, because it doesn't keep duplicities when same line was added in both branches - and I need the line twice (if it was added in both)

Example: Ours added:

A
- B
- X
- X

Theirs added:

C
- D
- X
- X

How it is merged:

A
- B
C
- D
- X
- X

What I need:

A
- B
- X
- X
C
- D
- X
- X

I understand why this is happen (union solves just the lines in conflict and X's aren't in any conflict) but how to gain what I need? Is there some other strategy, combination etc.?

1 Answers

Just delete the marker lines in conflict resolution and keep both. That gets you what you want here.

This is why conflict resolution is manual: the autoresolve gets very close to 100% of the cases right, but at the fringes there's just no telling who's going to need what this time. If you're doing a lot of these you can make a "keep both hunks" macro for your editor of choice, or do as @torek suggests and write a merge driver that identifies your exact cases and handles them correctly, it's just a question of what makes the work go quickest.

Related