Sub topic 6
Protecting Binary Files
This chapter assumes you know the basic Git workflow (clone, add, commit, status, diff) and the text attribute from the previous chapters. It covers how Git can damage binary files, how to prevent that, and how to check that your rules work.
Why binary files are at risk
Git's line-ending conversion replaces bytes. On commit, it can turn each CRLF pair (bytes 0D 0A) into a single LF (0A). On checkout, it can do the reverse. That is correct for source code. It corrupts a PNG, a PDF or a database file, because those formats use 0D and 0A as ordinary data.
With * text=auto, Git inspects each file's content and only converts files that look like text. The check is a heuristic. A file that contains NUL bytes is treated as binary. A binary file with no NUL bytes in the part Git examines can be misclassified as text and silently altered. Some Git versions also report the damage only as a changed checksum or a file that no longer opens.
The safest approach is to name your known binary types explicitly instead of relying on detection.
The binary macro
binary is a built-in macro attribute. Writing *.png binary is the same as writing:
*.png -diff -merge -textEach part does one job:
| Attribute | Effect |
|---|---|
| `-text` | Git never converts line endings for this path. |
| `-diff` | `git diff` shows "Binary files differ" instead of a byte-level patch. |
| `-merge` | Git does not attempt a line-based merge. A conflict leaves the file for you to resolve by choosing one version. |
Use the macro instead of writing -text alone. -text stops conversion, but Git would still try to produce text diffs and text merges for the file.
Marking binary types
Add one line per type, after any text=auto rule:
* text=auto
*.png binary
*.jpg binary
*.gif binary
*.pdf binary
*.zip binary
*.woff2 binaryOrder matters here. When several lines match a path, the last one wins for each attribute. If you put * text=auto below *.png binary, the catch-all rule would override -text for PNG files. Keep the general rule first and the specific rules after it.
Unusual cases
Some files are text-like but should still be left alone. Examples are minified bundles, generated files and fixtures that deliberately contain CRLF:
*.min.js -text
tests/fixtures/crlf-sample.txt -textHere -text is enough if you still want readable diffs. Use binary when diffs and merges should also be disabled.
You can also override a broader rule for one directory:
*.dat binary
firmware/*.dat -diff -merge -textThe second line is redundant with the first, so it adds nothing. Prefer one clear rule per type and avoid repeating the macro's expansion.
Verifying the result
Check the attributes Git resolves for a path:
git check-attr -a -- logo.pngExpected output looks like this:
logo.png: text: unset
logo.png: diff: unset
logo.png: merge: unset
logo.png: binary: setThen confirm the stored and working-tree states with git ls-files --eol:
git ls-files --eol -- logo.pngThe output for a protected file shows -text in the attribute column:
i/-text w/-text attr/-text logo.pngi/-text means the index copy is classified as binary. w/-text means the working-tree copy is too. If you see i/lf or i/crlf for a file that should be binary, Git is treating it as text and the rule is missing or overridden.
Fixing damage that already happened
Adding a rule does not repair files already committed with converted bytes. If a binary file was corrupted in an earlier commit, restore it from a known-good source, for example the original asset, and commit it again. Then run git add --renormalize . so Git re-evaluates tracked files under the new rules. The next chapters cover renormalization in detail.
Exercise
A repository has *.png binary on line 1 and * text=auto on line 2 of its .gitattributes. A teammate reports that PNG files sometimes get corrupted. What is the likely cause, and how do you fix it?
Show answer
The later * text=auto line overrides the text setting that binary applied, because the last matching line wins per attribute. Git may then treat a PNG as text and convert bytes. Move * text=auto to the top and keep *.png binary below it. Then run git check-attr -a -- file.png to confirm text is unset, and replace any file that was already corrupted.
Exercise
What do the three attributes in the binary macro do, and when would you use only -text instead?
Show answer
-text disables line-ending conversion, -diff suppresses text diffs, and -merge prevents line-based merging. Use only -text for text-like files, such as minified code or a fixture that must keep its CRLF endings, where you still want readable diffs and normal merges.