Do I really need to specify all binary files in .gitattributes
Asked Answered
C

2

19

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.

However, I have also read that Git is pretty good at detecting binary files, which makes me think 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 recommendations 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

Thanks to all for the answers, I've written up a blog post: .gitattributes Best Practices.

Clone answered 14/7, 2019 at 19:57 Comment(0)
B
21

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.

From git's source code:

#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);
}

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.

Barayon answered 20/7, 2019 at 1:5 Comment(1)
PNG files are normalized because of two line separators (1st DOS, 2nd Unix) in file header.Ingaingaberg
B
4

Git is, in general, good about detecting whether a file is text or binary, and so you may not explicitly need to set anything. Setting a default of * text=auto is a good idea regardless, as you point out.

However, if you or anyone working on the project is working with files in UTF-16, it's a very good idea to explicitly set the text attribute on those files, as well as the working-tree-encoding attribute, since Git will notice the NUL bytes in them and think of them as binary.

You should also specify any file type as binary that you think might be misdetected as text. For example, if you have some image format or file that consists only of printable ASCII bytes, Git might misdetect that as text. You'd want to specify those files explicitly to avoid confusion. Only you would know which files in your repository are likely to hit that issue.

Blueing answered 15/7, 2019 at 2:1 Comment(2)
Upvoted. How would I know if a file is being incorrectly modified to fix file endings. I guess the only way is if it was corrupted.Clone
If you add the file, and Git produces a diff (and not “Binary files….differ”) when you do git diff --cached, then Git thinks it's a text file. If you don't think that should be the case, then add a rule for that file type. You can also use git log -p or git show to look through the history if you have an existing repository.Blueing

© 2022 - 2024 — McMap. All rights reserved.