我读了Git 文档 https://help.github.com/en/articles/configuring-git-to-handle-line-endings这表明我可以显式地将某些文件设置为文本,因此它们的行结尾会自动更改或作为二进制文件以确保它们不受影响。
然而,我也读到 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
感谢大家的回答,我写了一篇博文:.gitattributes 最佳实践 https://rehansaeed.com/gitattributes-best-practices/.
Git 将检查文件的前 8,000 个字节,看看它是否包含 NUL 字符。如果是,则该文件被假定为二进制文件。
From git的源代码 https://github.com/git/git/blob/9c9b961d7eb15fb583a2a812088713a68a85f1c0/xdiff-interface.c#L187-L193:
#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);
}
对于文本文件,除非您出于某种原因故意插入 NUL 字符,否则它们将被正确猜测。对于二进制文件,前 8,000 个字节很可能至少包含一个实例。
在大多数情况下,您不需要显式声明文件的类型(我认为我从来没有这样做过)。实际上,如果遇到问题,只需声明一个特定文件即可。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)