'.gitattributes: override settings for a single file

I have a directory of .txt files that I would like to treat as binary files. This prevents git from changing the line endings of those files so the checksum is consistent on all systems.

However, I have one .txt file that I would like to keep as text. If I create a .gitattributes file like:

*.txt binary
foo.txt text

foo.txt is still treated as binary and git diff doesn't display changes properly. It seems like negation isn't supported in .gitattributes like it is in .gitignore. Is there any other way to make this work?



Solution 1:[1]

binary is what the gitattributes documentation refers to as a macro attribute:

Setting the "binary" attribute also unsets the "text" and "diff" attributes as above.

So binary literally means -text -diff. You then override the -text setting for foo.txt with the second line, but you have not overridden the -diff setting. Changing the second line to:

foo.txt text !diff

would restore the default unset-ness for diff, while:

foo.txt text diff

will force diff to be set (both will presumably result in a diff, since Git has presumably not previously been detecting foo.txt as binary).

(Note that there's no direct way to "undo" a macro attribute other than piecemeal. You could define your own macro attribute that undoes each piece of some existing macro attribute, e.g., define one that means !text !diff, which will then take away binary.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 torek