1

We have a file that is usually being generated automatically, and therefor it has a comment in the beginning of the file indicating the date of when the file was generated, i.e:

// generation date 01/10/2020

When we use git rebase, between two branches that have modified this automatically generated file (either manual change, or maybe regenerated the file), even if all the content of the file is the same between the two branches - except the specific date comment in the beginning of the file - git marks this as conflict, and it is really annoying (because there are many files like that, not only one).

I would like to have git automerge this specific conflict. Maybe by providing a certain regex so if the conflict matches the regex, git can take for instance the other branch's version (because we don't really care about the date during merging).

I know we could just not generate this comment - and then avoid future conflicts, but we would like to keep the date comment.

We are using different tools in order to merge, so we prefer a git solution, and not tool specific solution.

Any ideas on how to solve our problem?

Thanks.

shush
  • 11
  • 2
  • 6

1 Answers1

1

There is no built in solution.

The closest you can get using what Git provides directly is to write a merge driver. A merge driver takes three input files—merge base, and the other two files—and produces the merged result. You can do this by:

  • reading the three input files;
  • using your regular expression (or whatever) to identify lines that you'd like to auto-merge;
  • remove or replace those lines—this is the tricky part—and use git merge-file to merge the remaining lines; and
  • re-insert the autogenerated merge in the appropriate places in the result of git merge-file.

Your exit status, from this driver, should be the exit status of git merge-file (which is already zero or nonzero appropriately), or forced to nonzero if you want your own auto-merge to be regarded as a conflict.

For more information on writing a merge driver, see the gitattributes documentation.

torek
  • 448,244
  • 59
  • 642
  • 775