How do I set a default merge strategy specific to a file with Git?
Asked Answered
G

1

8

This question is not iOS-specific, but I'm including the actual use case here for clarity

I run an iOS project called Foobar, obviously kept under version control. Amongst the project files in the iOS project environment there's something called Foobar-Info.plist, an XML file which stores interesting information about the project, like versions and the number of builds I've made.

Every time I build the project, I increment the build count stored in to this file like so:

...
<key>BuildCount</key>
<string>2203</string>
<key>CFBundleShortVersionString</key>
<string>0.0.4</string>
<key>CFBundleVersion</key>
<string>0.0.4-release/0.0.4.2203</string>
...

where '0.0.4-release' is a git-flow branch name, and 2203 is a build number. This is used in the CFBundleVersion field, so that it's really obvious to me where the build came from.

Imagine in another branch, I've made a lot of progress since the release and the same fields look like:

...
<key>BuildCount</key>
<string>2754</string>
<key>CFBundleShortVersionString</key>
<string>0.0.4</string>
<key>CFBundleVersion</key>
<string>0.0.4-feature/add-quux-and-baz.2754</string>
...

Let's say I'm done with release, and it's merged into the mainline develop branch.

The problem

In order to get the latest changes made in the release branch, I want to rebase the feature branch onto develop.

when this happens, Foobar-Info.plist will cause a conflict at every commit in the rebase process. This is because the build numbers will have incremented for every commit; I must manually merge by choosing the lines manually specify the lines with the latest version. Note that the base version of the 3-way diff will also be different.

<string>2000</string>  // Base
<string>2754</string>  // Local   ** always pick this one; it's the latest
<string>2203</string>  // Remote

How can I tell Git that for one specific file, Foobar-Info.plist, I want it to resolve my conflicts for me by taking the latest change?

The file must be kept under source control. I want to keep the BuildCount because it's a cool indication of effort put into my project and I want to keep it! I know that the figure only indicates the minimum number of builds done, but that'll be good enough for me.

Gremlin answered 17/4, 2014 at 8:46 Comment(4)
I found #929146, which could be an answer to my question.Gremlin
'Copy merge' ISN'T what I want. I sort of want a COPY MERGE FOR CONFLICTS ONLY. Other changes should still be applied and merged as normal.Gremlin
Another thing: it's not copy merge because the latest change is not necessarily the local branch: it's conceivable that develop has commits from other features, which has a greater build number from the branch being rebased.Gremlin
I also found this: #7607625 they wrote their own merge driver to handle their case.Gremlin
M
2

I realize this is a late answer, but if I understand your question, what you're looking for is a .gitattributes file.

You can specify merge strategies for specific files or folders with relative paths from the .gitattributes file, which looks like this:

* text=auto
path/to/your/file.plist merge=union
Mastoid answered 12/6, 2015 at 1:16 Comment(1)
merge=union combines both versions so you end up with <key>CFBundleVersion</key> <string>100</string> <string>101</string>Wrath

© 2022 - 2024 — McMap. All rights reserved.