Sub topic 4
Letting Git detect text with text=auto
This chapter assumes you know what LF and CRLF are, that you can create and commit files in a repository, and that you have seen the .gitattributes file format (one pattern followed by attributes per line). It covers the text attribute, with a focus on text=auto.
The four states of the text attribute
The text attribute controls whether Git applies line-ending conversion to a path. It has four states.
| Setting | Meaning |
|---|---|
| `text` | Always treat the file as text. Normalize CRLF to LF when staging. |
| `-text` | Never treat the file as text. No conversion happens. |
| `text=auto` | Let Git decide per file. Normalize only if Git judges it to be text. |
| unspecified | No rule applies. `core.autocrlf` decides instead. |
"Normalize" means that the repository (the index) stores LF. On checkout, Git converts to the ending chosen by eol, core.eol or core.autocrlf, but only for files it treats as text.
The most common rule
One line covers most repositories:
* text=autoThis matches every path. For each file that Git stages, it inspects the content and decides whether the file is text. If it is, CRLF sequences are converted to LF before the blob is stored. If it is not, the bytes pass through untouched.
How Git decides
Git looks at the file content, not the extension. A file is treated as binary if it contains a NUL byte, or if it contains a lone CR (a carriage return not followed by LF). Otherwise it is text. This heuristic is the same one Git uses for diffs and for git grep.
The second decision is easy to miss. If a text file is already committed with CRLF, text=auto does not convert it. Git keeps the existing bytes so that adding the rule does not silently rewrite old files. The result is that adding * text=auto to an old repository changes nothing for files that already contain CRLF. You fix those with renormalization, which a later chapter covers.
Why auto instead of plain text
Plain text forces conversion on every matching file. If a file is actually binary, such as a PNG, converting bytes that happen to look like 0D 0A would corrupt it. With text=auto, Git's detection protects most binary files. It is not perfect, though, so you should still mark known binary types explicitly (the binary files chapter covers this).
Use plain text when you know a path is text and want no guessing. Use text=auto as the catch-all default.
What happens on checkout
Normalization on commit is only half the behavior. On checkout, the working tree ending for a text file depends on, in order:
- An
eolattribute for the path, if set. core.autocrlf, if set totrueorinput.core.eol, which defaults to the platform's native ending.
With * text=auto and no other settings, a Windows checkout gets CRLF and a Linux checkout gets LF. The repository always holds LF. This is the reason the rule keeps diffs clean across platforms.
Verifying the result
You can confirm that a path matches the rule and how Git classifies it:
git check-attr text -- README.md
git ls-files --eol README.mdThe first command should print README.md: text: auto. The second prints something like i/lf w/crlf attr/text=auto README.md. Here i/lf is the index ending and w/crlf is the working tree ending.
Common mistake: a rule that also hits binaries
This setup is fragile:
* text=auto
*.dat textThe second line forces text handling on .dat files. If any of them is binary, conversion can corrupt it. Prefer -text or binary for files you know are not text.
Exercise
Exercise
Your repository has an old file notes.txt committed with CRLF line endings. You add * text=auto to .gitattributes and commit that file. A teammate checks out the repository on Linux. Does git ls-files --eol notes.txt show i/lf or i/crlf for it, and why?
Show answer
It still shows i/crlf. With text=auto, Git does not convert a file whose index copy already contains CRLF, so the stored bytes stay the same. The file only becomes i/lf after you renormalize it (for example with git add --renormalize .) and commit the result.