5

Possible Duplicate:
Locking binary files using git version control system

I am committing a file which should never be merged, but overwritten (perhaps with a warning) when other people pull. Is there a way of accomplishing this?

Community
  • 1
  • 1
Christian Neverdal
  • 5,655
  • 6
  • 38
  • 93
  • 1
    In what way is it "pure"? Is it a derived file, or a binary file perhaps? – cmbuckley Feb 11 '12 at 23:09
  • I couldn't think of another word. Unmergable, perhaps? Yes, for our purposes let us say it is a derived file. – Christian Neverdal Feb 11 '12 at 23:22
  • This smells a lot like a 'I need locks in Git' argument, albeit worded differently. What you are asking for goes against the very spirit of a DVCS. – Perception Feb 11 '12 at 23:24
  • Is the file derived from the project itself? Maybe don't check it in and include a script to generate it each time the project is checked out. Or treat it as a submodule. – rwilliams Feb 11 '12 at 23:28
  • 1
    What do you mean by merged or overwritten when *people check it out*? If you're talking about cloning, or checking out branches, that doesn't involve merging. – Cascabel Feb 12 '12 at 00:51

1 Answers1

5

It's a little unclear what your actual situation is. The first question you should ask yourself is whether the file actually needs to be tracked - unmergeable files are often derived files, as you suggest in the comments, and therefore don't need to (and shouldn't) be tracked.

If you really do need to track it, is it a binary file? Git doesn't try to merge binary files - they always show up as merge conflicts. Git is good about detecting binary files, so in this case, you're probably safe. If it's not a binary file, then you still force Git to deal with it appropriately. In a .gitattributes file, add something like:

path/to/file merge=binary

This will direct it to treat it as a binary file for purposes of merging. You could also, if you wanted, define a custom merge driver. Use merge=my_merge_driver in the gitattributes file, then in your gitconfig add something like:

[merge "my_merge_driver"]
    name = descriptive name
    driver = my_script %O %A %B

The three arguments for the script are the common ancestor version (O for original), the current branch's version (A) and the other branch's version (%B) - they're temporary files, and the merge driver is supposed to do the merge and leave the result in %A. See man gitattributes for more about this.

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • Would this work even on a fast forward merge? – Carl Oct 16 '12 at 20:49
  • @carleeto The merge driver is only invoked if the file actually needs to be merged, which is not the case in a fast-forward merge. But unless you're doing something different from the OP here, that doesn't matter - the fast forward merge is already overwriting the original version with the merged version. – Cascabel Oct 16 '12 at 22:51
  • Thanks for that. I was just trying to keep some annoying build products that were in the main repo from distracting me in my branch :) – Carl Oct 17 '12 at 04:34