Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can git's .gitattributes treat all files as binary except a few exceptions?

Tags:

I have a particular need for git to treat most file extensions as binary except a few extensions.

I'd like to treat all file extensions as binary, .pdf .doc .xls etc., except plain text files such as .txt .rb .py etc.

I've tried configuring .gitattributes like below to see how this might work:

# cat .gitattributes 
* binary
*.txt text

I thought perhaps the order in the configuration file would matter but it doesn't appear to. With the above configuration all files are still treated as binary.

Is there a way to configure .gitattributes or git any other way to treat all files one way, as binary, except for a few exceptions?

Update 1:

I tried the .gitattributes described below. It works!

# cat .gitattributes 
*.txt crlf diff
* binary


# git diff
diff --git a/file b/file
index d929b94..bee5cb1 100644
Binary files a/file and b/file differ

diff --git a/file.txt b/file.txt
index 632ae98..93d22b0 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1,3 @@
 Hey this is a .txt file
+Adding another line
+A new line

Update 2:

I believe crlf and text are the same i.e. the two below configurations for .gitattributes are the same:

# cat .gitattributes 
*.txt crlf diff
* binary

# cat .gitattributes 
*.txt text diff
* binary
like image 645
caleban Avatar asked Nov 24 '10 07:11

caleban


2 Answers

binary is a macro setting the attribute crlf and diff (actually here unsetting them)
See "USING ATTRIBUTE MACROS" from the .gitattribute man page.

Once an attribute is set or unset, if cannot be changed by a subsequent rule.

So you could try:

* -text
*.txt crlf diff

That way, crlf and diff being set for *.txt files, they won't be unset by the binary macro for those same *.txt files, while they will be unset for all the other files.

For LF or auto:

*.txt text eol=lf
#
*.txt text=auto

From the 2009 commit b9d14ff, those rules should go:

  • from the more general ones
  • to the more specific ones.
    ("a later line overrides an earlier line")
like image 93
VonC Avatar answered Nov 02 '22 07:11

VonC


git has no concept of "binary" and "text" files. It's all defined as a set of attributes which designate how should we do merges, diffs, CR/LF conversions, handle whitespaces, apply filters and zillions of other things.

binary and syntax like

*.o binary
is actually macro-based, i.e. binary is a macro that expands to a whole lot of various attributes that designate merging, diffing, CR/LF handling, etc.

There is no text macro as far as I see. binary expands to -crlf -diff, so disabling binary and going back to text-style processing seems to be crlf diff.

like image 34
GreyCat Avatar answered Nov 02 '22 07:11

GreyCat