Git Attributes and Line Endings
Separate storage from checkout
A line feed (LF, byte 0A) and carriage return plus line feed (CRLF, bytes 0D 0A) represent line boundaries. They can look identical in an editor but differ as bytes. For normalized text, Git stores LF in the index and committed blob; checkout may produce CRLF if requested. .gitattributes is a tracked policy file, unlike local core.autocrlf. In the practice project we use explicit attributes to make the intended behavior independent of the host default.
printf '%s\n' '*.txt text eol=lf' '*.bat text eol=crlf' '*.bin -text' > .gitattributesprintf 'Water every 3 days\r\n' > notes.txtprintf 'echo hello\r\n' > run.batprintf 'A\r\nB\000' > raw.bingit add -- .gitattributes notes.txt run.bat raw.binInspect index and working bytes
text requests normalization, text=auto lets Git classify content, and -text disables line-ending conversion (not all other filters). eol=lf or eol=crlf selects checkout style for text. core.autocrlf provides defaults when attributes leave conversion unspecified; core.safecrlf can warn or reject irreversible conversions under its current settings. Inspect attributes and the index/worktree line-ending report. The fixture stages LF blobs for notes.txt and run.bat; raw.bin retains its bytes. Adding does not itself rewrite the working files. A fresh checkout demonstrates both explicit styles.
git check-attr text eol -- notes.txt run.bat raw.bingit ls-files --eolgit diff --cached --statMigrate old content deliberately
Adding attributes does not retroactively change existing commits. In a clean practice repository with a previously committed CRLF text file, add the new policy and use git add --renormalize on tracked paths. This reapplies the clean process, which may include configured filters, so review trusted configuration first. Review staged changes and keep this migration separate from functional edits. Do not run it blindly in a dirty repository. Attribute precedence can include .git/info/attributes and nested policies; a later matching rule can override a particular attribute.
printf '%s\n' '*.txt text eol=lf' > .gitattributesgit add -- .gitattributesgit add --renormalize -- notes.txtgit diff --cached --statgit ls-files --eolPractice
Predict the index and fresh checkout bytes for run.bat and raw.bin. Explain why a full-file diff may indicate line-ending changes, and why binary data should not be forced through text normalization. Inspect the old snapshot and new staged blob separately.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Attributes and Line Endings — knowledge check
Reason about configuration, platform behavior and execution boundaries.
With text eol=crlf what is the normalized index line ending?
Which attribute disables end-of-line conversion?
Does git add necessarily rewrite the working file to LF?
What is a safe approach to a legacy normalization migration?
Does a new attributes file change old commits?