Chapter 44: Git .gitattributes

1. What exactly is .gitattributes?

It is a normal text file (just like .gitignore) that lives in your repository (usually in the root).

Git reads it automatically and applies special rules to matching files / patterns.

The most common things people set in .gitattributes:

Purpose Typical line in .gitattributes What it actually does
Force text files to use LF endings *.txt text eol=lf *.md text eol=lf Always checkout with LF, commit with LF — prevents Windows CRLF corruption
Force Windows-style CRLF *.bat text eol=crlf *.cmd text eol=crlf Windows batch scripts must keep CRLF
Treat files as binary (no diff) *.png binary *.jpg binary *.zip binary Git never tries to diff them (saves space, avoids corruption)
Enable word-diff / better blame *.json text *.xml text Treat as text even if they look binary — enables line-by-line blame & readable diffs
Delta compression for large text *.min.js text diff Allow delta compression even for minified files
Custom diff driver *.ipynb diff=ipynb Use special Jupyter notebook diff tool

2. The two most important attributes (used in 95% of real projects)

Attribute Meaning Most common real-world usage (2026)
text This is a text file — Git should do line-ending conversion *.sh text eol=lf *.py text eol=lf *.js text *.json text
eol=lf / eol=crlf Force checkout line endings to LF or CRLF *.sh text eol=lf (Unix scripts must have LF) *.bat text eol=crlf (Windows scripts)
binary Treat as binary — no line-ending conversion, no diff, no merge *.png binary *.jpg binary *.pdf binary *.zip binary *.exe binary

3. Real Example – Create & Use .gitattributes Right Now

Step 1 – Make a small demo project

Bash

Step 2 – Create mixed files

Bash

Step 3 – Create .gitattributes

Create file .gitattributes in root:

text

Step 4 – See the magic

Bash

Git now knows:

  • README.md, app.js, config.json → treated as text with LF
  • run.bat → treated as text but with CRLF
  • image.png → treated as binary (won’t show in diff)

Commit:

Bash

Step 5 – Test line-ending behavior

On Windows machine → checkout repo → open README.md in Notepad → it shows LF endings (Git converted automatically)

On Linux/macOS → same file → LF (no change)

If someone changes run.bat on Linux → Git forces CRLF on checkout → Windows users happy.

4. Common .gitattributes Patterns (copy-paste ready – 2026)

Minimal modern JavaScript/TypeScript project

text

Python project

text

5. Common Gotchas & Fixes (2026 reality)

Problem Reason & Fix
File still tracked after adding to .gitattributes Already committed → git rm –cached filename → commit
Line endings still wrong Use text eol=lf or text=auto — text=auto lets Git guess but prefers LF
Huge repo because of images/PSDs Add *.psd binary + use Git LFS for large files
Teammate on Windows has CRLF everywhere Add * text=auto eol=lf at top + commit .gitattributes
Diff shows entire minified JS file changed Add *.min.js text so Git treats it as text

Got the .gitattributes feeling now?

.gitattributes = Git’s per-file rule book — controls line endings, binary/text treatment, diff behavior, and more

It’s one of the first three files every serious project creates (along with .gitignore and README.md).

Next?

  • Want a ready-made .gitattributes for your specific stack (React, Python, Java, etc.)?
  • See how to use Git LFS with .gitattributes for large files?
  • Or wrap up with one final big summary of all Git concepts we covered?

Just tell me — we’ll finish strong. You’ve now gone from zero to advanced Git — incredible journey! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *