1

I have a couple of files (a pom.xml and an appengine-web.xml (GAE config file)) that I want to keep in different states in different branches of the same project.

Similar questions have been asked before and two of the solutions I've seen suggested before are:

  • git merge --no-commit from-branch, and,
  • Create file .gitattributes in the same dir, with this line: filename.ext merge=ours (paraphrasing)

Both these only protect the file in the current branch from merges if there is a conflict during merge. In case of an FF merge, the version in the current branch gets overwritten. I want these files to not be overwritten even in the case of an FF merge.

Isn't there a way to protect the version in the current branch from ever being overwritten by a merge, unless invoked by a file-specific command?

I am currently resorting to not checking in the per-branch files, putting them on the stash and then restoring from there when I come back to the branch. Is there a more efficient and less fault-prone way?

markvgti
  • 4,321
  • 7
  • 40
  • 62

1 Answers1

2

An alternative approach is to:

  • not track pom.xml
  • track pom.xml.master and pom.xml.mybranch: that way, any merge won't affect them, since they are named differently.

Then, you can version and track:

  • one pom.xml per branch (with a naming convention following said branch).
  • a script able to take the right pom.xml.<abranch> file depending on the current branch, and generate the pom.xml file (which remains untracked, private)
  • a .gitignore which ignores the resulting generated pom.xml file
  • a .gitattribute declaring a smudge content filter (see below)

The generation of the actual pom.xml is automated through a content filter driver, using a .gitattributes declaration.

https://i.stack.imgur.com/tumAc.png
(image from "Customizing Git - Git Attributes" from "Pro Git book"))

Once you declare that content filer driver in your local config, it will automatically, on git checkout, generate your pom.xml file for you.
See a complete example in "Best practice - Git + Build automation - Keeping configs separate".

Repeat the same idea for the appengine-web.xml file.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks! I'll study this and try it out, seems like it could be the solution for me. – markvgti Jul 15 '17 at 06:16
  • +1 - that "smudge filter to make an untracked file" trick is pretty nice, if you don't have a build system that can do it for you (or don't want to assume or invoke one) – torek Jul 15 '17 at 15:37
  • Confused. Does the smudge filter get the name of the file to be "smudged"? Does a clean filter also have to be defined? Can smudge filter be run on a file that's not tracked? Created the branch-specific files, but not getting anywhere with writing out a pom.xml (in my case I simply need to copy pom.xml. to pom.xml). – markvgti Jul 19 '17 at 13:35
  • @markvgti Does the smudge filter get the name of the file to be "smudged"? No: it is applied to what you define in your `.gitattributes.` (in your case: `pom.xml.* filter=my_smudge_filter`) "Does a clean filter also have to be defined?" No: you don't modify any tracked file, so you don't have to clean any track file. You use the smudge to generate an untracked file. "Can smudge filter be run on a file that's not tracked?": No, hence my suggestion to track `pom.xml.xxx`, and generate the actual `pom.xml` from there. – VonC Jul 19 '17 at 13:54