我真的需要在.gitattributes中指定所有二进制文件吗 [英] Do I really need to specify all binary files in .gitattributes

查看:66
本文介绍了我真的需要在.gitattributes中指定所有二进制文件吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读 Git文档这表明我可以明确地将某些文件设置为文本,因此它们的行尾会自动更改为二进制文件,以确保它们不被更改.

I've read Git documentation that shows that I can explicitly set certain files to be treated as text, so their line endings are automatically changed or as binary to ensure that they are untouched.

但是,我还读到Git在检测二进制文件方面非常擅长,这使我觉得这是不需要的.所以我的问题是,我是否真的需要为存储库中的每个文件扩展名指定这些显式设置?我看到有人建议对所有图像文件扩展名都这样做.

However, I have also read that Git is pretty good at detecting binary files, which makes me thing this is not needed. So my question is do I really need to specify these explicit settings for every single file extension in my repository? I've seen some recommend to do so for all image file extensions.

# 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

推荐答案

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

Git will check the first 8,000 bytes of a file to see if it contains a NUL character. If it does, the file is assumed to be binary.

来自 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);
}

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

For text files, unless you intentionally insert a NUL character for some reason, they'll be correctly guessed. For binaries, it's more than likely that the first 8,000 bytes will contain at least a single instance.

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

For the most part, you shouldn't need to declare a file's type explicitly (I don't think I ever have). Realistically, just declare a specific file if you run into an issue.

这篇关于我真的需要在.gitattributes中指定所有二进制文件吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆