41

For merging I use this to "keep mine"

git merge -X ours foo

and this for "keep theirs"

git merge -X theirs foo

However on my latest merge it looks best to keep both sides. Does Git have a "strategy" for this, to avoid manually editing the file?

Zombo
  • 1
  • 62
  • 391
  • 407

3 Answers3

41

There is no 'merge strategy' for resolving these conflicts.

However, if you truly want a conflict like:

<<<< ours
Foo
=========
Bar 
>>>> theirs

to resolve to

Foo
Bar

then you can configure the 'merge driver'. From the gitattributes man page:

union

Run 3-way file level merge for text files, but take lines from both versions, instead of leaving conflict markers. This tends to leave the added lines in the resulting file in random order and the user should verify the result. Do not use this if you do not understand the implications.

Add a line to .gitattributes to use this:

*.whatever merge=union
Dwight Holman
  • 1,570
  • 14
  • 15
  • 17
    Union does not map to "keep both". Union is "keep both" minus intersection. This will fail horribly when trying to "keep both" on XML like resource files. – Ilia G Oct 11 '13 at 13:56
  • In case when 2 diff hunks conflict, they seem to be kept both, fully. – max630 Jan 27 '16 at 20:04
  • 3
    but there's no way to add this as an option (if we don't consider editing `.gitattributes` to be an 'option') that can be called specifcally for conflicts that you want to merge this way? these are so common it would be so nice to have a way to one-line-cli them (multiple imports / functions / properties added to the same file and order doesn't matter) – Damon Oct 26 '18 at 13:49
  • 2
    What is the ".*whatever" supposed to connote here? It's clear at all – Brian Peterson Feb 26 '20 at 21:44
  • 1
    Also, at what point do I add this line to .gitattributes, and then what do I after (or before) to get the "union" merge to occur? – Brian Peterson Mar 09 '20 at 16:47
  • @BrianPeterson `whatever` is the file extension - see [.gitignore pattern format](https://www.git-scm.com/docs/gitignore#_pattern_format) which is the same. – dotnetCarpenter Jan 30 '22 at 16:56
  • @dwight-holman I can't get your solution to work. Would you mind taking a look at https://stackoverflow.com/questions/71369712/how-to-use-git-merge-union? (https://stackoverflow.com/q/71369712/205696) – dotnetCarpenter Mar 06 '22 at 11:11
  • Great answer, been looking through git docs for this exactly – ShaMan123 Oct 16 '22 at 06:20
10

What about this one?

grep -v -e'^<<<<<<<' -e '^>>>>>>>' -e'=======' filename.txt > filename.tmp

mv filename.tmp filename.txt

james dupont
  • 101
  • 1
  • 3
  • This will omit lines that were the same in theirs and ours. I guess it's called the intersection. At the point of merge where you have the above strings ("<<<" etc) git has already removed "duplicate" lines that I actually want to keep. – Stan Jul 26 '21 at 14:49
-1

Sometimes a large chunk of code needs conflict resolution. When I want to keep both, I do this:

  1. Copy the whole code chunk. So, two instances are there.
  2. Remove the HEAD or ours sections on the 1st instance.
  3. Remove the to-be-merged or their sections on the 2nd instance.
Megidd
  • 7,089
  • 6
  • 65
  • 142