Lesson

Git .gitattributes

10 min reading
Free Course

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=lf normalizes text files to LF on commit and checkout.
  • *.bat text eol=crlf keeps Windows batch scripts with CRLF so Windows executes them reliably.
  • *.png binary tells 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.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol
Git Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum