Git .gitattributes
Git .gitattributes: Enforcing Cross-Platform File Rules
A .gitattributes file stored in your repository root defines per-file-type rules for line endings, binary diff behavior, merge strategies, and release archive exports. Unlike local git config settings that each developer must set manually, .gitattributes travels with the repository so every contributor gets identical behavior automatically.
Standardizing Line Endings Across Windows, macOS, and Linux
Line ending mismatches (CRLF vs LF) create phantom diffs where every line in index.html or styles.css looks changed. Set explicit rules in .gitattributes:
* text=auto eol=lf
*.bat text eol=crlf
*.sh text eol=lf
*.svg text
*.png binary
*.woff2 binary
* text=auto eol=lfnormalizes text files toLFon commit and checkout.*.bat text eol=crlfkeeps Windows batch scripts withCRLFso Windows executes them reliably.*.png binarytells Git not to run line-ending conversions or text diffs on images.
Re-Normalizing Existing Files After Adding .gitattributes
Adding .gitattributes fixes future commits, and you can normalize already-committed files cleanly in one pass:
git add --renormalize .
git commit -m "Normalize line endings via .gitattributes"
Hiding Generated Diffs and Skipping Dev Files in Archives
Keep pull request diffs readable by marking minified files or lockfiles as binary/diff-ignored, and exclude test folders from git archive release tarballs:
package-lock.json linguist-generated=true
tests/ export-ignore
.github/ export-ignore
Common .gitattributes Mistakes
Forgetting to mark custom binary formats (such as .pdf or .sqlite) as binary can cause Git to corrupt them during automatic text normalization on Windows. Always declare binary extensions explicitly.
Frequently Asked Questions
What is the difference between .gitignore and .gitattributes?
.gitignore tells Git which files not to track at all. .gitattributes configures how Git handles files that are tracked (line endings, diffs, merges, and LFS).
Does .gitattributes override a developer's core.autocrlf setting?
Yes. Explicit eol=lf or eol=crlf rules in .gitattributes take precedence over local core.autocrlf settings, ensuring consistent line endings across the entire team.
Where should .gitattributes be placed?
Usually at the root of your repository alongside .gitignore, though subdirectories can hold their own .gitattributes files to override rules locally.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With