2

I'm trying to script merging branches with pom.xml files that usually only conflict with changes in the version number but often have other non-conflicting changes in them. If I use "git checkout --ours" even non-conflicting changes are skipped is there a workaround for this?

Example of what I mean:

<xml>
   <thing>
<<<<<<< HEAD
        <version>1.1.0</version>
=======
        <version>1.2.0</version>
>>>>>>> origin/remote_branch
    </thing>
    <other>
      <!-- merged changes -->
    </other>
</xml>

I want to achieve with something like "git checkout --ours"

<xml>
   <thing>
        <version>1.1.0</version>
    </thing>
    <other>
      <!-- merged changes -->
    </other>
</xml>

Is that possible?

Ivan Wills
  • 151
  • 9

1 Answers1

4

That should be done with a merge strategy option: git merge branch -X ours:

ours:

This option forces conflicting hunks to be auto-resolved cleanly by favoring our version. Changes from the other tree that do not conflict with our side are reflected to the merge result.
For a binary file, the entire contents are taken from our side.

This is an option to the recursive merge strategy.

If you want to apply that option only to certain files, while keeping the default merge resolution for the other files, you can use a custom merge driver in order to call git merge-file --ours on those specific files. You declare it in the config file.

That merge driver is associated to files in a .gitattributes:

pom.xml merge=keepours
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This somewhat resolves the issue except in the case where I don't want the whole merge to be "ours" I only want specific files (eg pom.xml files to be ours but .java files to be manually merged) – Ivan Wills Mar 13 '15 at 07:52
  • @IvanWills true, but that wasn't your original question. – VonC Mar 13 '15 at 07:54
  • @IvanWills I have edited the answer to propose an approach which would fit your merge process. – VonC Mar 13 '15 at 07:57