Philip Pittle
Philip Pittle

Reputation: 12295

GitAttribute to force manual merging specific file

My repository has a version.json where we store the major and minor version of the code base.

How can I configure git to not automatically merge changes to that file?

For example, the version.json file has { "version": "1.2"} and I have two feature branches that bump it to "1.3", I want the last merge to conflict:

A--B-----PR1--PR2
   | \  /     /
   |  F1     /
   F2--------

Both F1 and F2 make the same change to the version.json file, they both edit it to be { "version: "1.3" }. The merge commit PR1 is fine, but I'd like the merge PR2 to require a manual review of the version.json file (because we'll need to set the version to 1.4).

I tried creating a .gitattribute to treat the version.json file as binary, but git seemed to still be able to merge the results.

Upvotes: 0

Views: 42

Answers (1)

bk2204
bk2204

Reputation: 76409

I don't think you're going to be able to find a solution to do what you want. You can use the merge attribute to control how files that differ are merged, but in your case you have two identical files.

When Git does a merge of two different commits, it compares each file in the tree in both heads that it's merging (in this case, PR1 and F2), taking into account renames. If the files are identical in both cases (that is, the blobs have the same hash), then it will merge them by just taking the version that's in both heads as the result without considering anything else.

This is the case you have: both sides have the same file in an identical state, so Git doesn't consider that any merge needs to be done, and consequently doesn't invoke any merge code or honor any attributes related to merges. It doesn't matter that your trees are otherwise different: the differing files will be merged, but the identical files won't have the merge machinery invoked.

If you had two files that differed, you could use version.json -merge to cause them to conflict any time they changed, but that will have no effect on identical files.

If the semantic you're going for is to bump the version on each commit or merge, then it's better to compute the version by using Git and some sort of script rather than changing the file in the repository each time.

Upvotes: 2

Related Questions