我真的需要在 .gitattributes 中指定所有二进制文件吗

Muh*_*eed 7 git newline lf core.autocrlf

我已经阅读了Git 文档,该文档表明我可以明确地将某些文件设置为文本,因此它们的行尾会自动更改或作为二进制文件以确保它们不变。

但是,我也读到 Git 非常擅长检测二进制文件,这让我觉得这不是必需的。所以我的问题是我真的需要为存储库中的每个文件扩展名指定这些显式设置吗?我已经看到一些建议为所有图像文件扩展名这样做。

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
Run Code Online (Sandbox Code Playgroud)

jhp*_*ING 10

Git 将检查文件的前 8,000 个字节以查看它是否包含 NUL 字符。如果是,则假定该文件是二进制文件。

git 的源代码

#define FIRST_FEW_BYTES 8000
int buffer_is_binary(const char *ptr, unsigned long size)
{
    if (FIRST_FEW_BYTES < size)
        size = FIRST_FEW_BYTES;
    return !!memchr(ptr, 0, size);
}
Run Code Online (Sandbox Code Playgroud)

对于文本文件,除非您出于某种原因有意插入 NUL 字符,否则它们将被正确猜测。对于二进制文件,前 8,000 个字节很可能至少包含一个实例。

在大多数情况下,您不需要显式声明文件的类型(我认为我从来没有)。实际上,如果遇到问题,只需声明一个特定的文件。