除了少数例外,git 的 .gitattributes 可以将所有文件视为二进制文件吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4264447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:46:56  来源:igfitidea点击:

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

git

提问by caleban

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

我特别需要 git 将大多数文件扩展名视为二进制文件,除了少数扩展名。

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

我想将所有文件扩展名视为二进制、.pdf .doc .xls 等,除了纯文本文件,如 .txt .rb .py 等。

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

我试过像下面这样配置 .gitattributes 来看看它是如何工作的:

# 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?

有没有办法配置 .gitattributes 或 git 以任何其他方式将所有文件以一种方式处理为二进制文件,除了少数例外?

Update 1:

更新 1:

I tried the .gitattributes described below. It works!

我尝试了下面描述的 .gitattributes。有用!

# 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:

更新 2:

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

我相信 crlf 和 text 是相同的,即 .gitattributes 下面的两个配置是相同的:

# cat .gitattributes 
*.txt crlf diff
* binary

# cat .gitattributes 
*.txt text diff
* binary

回答by VonC

binaryis a macro setting the attribute crlf and diff (actually here unsetting them)
See "USING ATTRIBUTE MACROS" from the .gitattributeman page.

binary是一个设置 crlf 和 diff 属性的宏(实际上在这里取消设置它们)
请参阅手册页中的使用属性宏” 。.gitattribute

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

一旦设置或取消设置属性,则后续规则无法更改。

So you could try:

所以你可以试试:

* binary
*.txt crlf diff

That way, crlfand diffbeing set for *.txtfiles, they won't be unset by the binary macro for those same *.txtfiles, while they will be unset for all the other files.

这样,crlf并且diff*.txt文件设置时,它们不会被那些相同*.txt文件的二进制宏取消设置,而对于所有其他文件,它们将被取消设置。

From the 2009 commit b9d14ff, those rules should go:

2009 年提交 b9d14ff 开始,这些规则应该是:

  • from the more general ones
  • to the more specific ones.
    ("a later line overrides an earlier line")
  • 从更一般的
  • 到更具体的。
    (“后面的一行覆盖了前面的一行”)

回答by GreyCat

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.

git 没有“二进制”和“文本”文件的概念。所有这些都被定义为一组属性,这些属性指定我们应该如何进行合并、差异、CR/LF 转换、处理空格、应用过滤器和无数其他事情。

binaryand syntax like

binary和语法像

*.o binary
实际上是基于宏的,即binarybinary是一个宏,它扩展到许多指定合并、差异、CR/LF 处理等的各种属性。

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

text据我所知,没有宏。binary扩展为-crlf -diff,因此禁用二进制并返回到文本样式处理似乎是crlf diff