TExcelTAD
Home › Git › Git .gitattributes

Git .gitattributes

While .gitignore decides which files Git tracks, .gitattributes decides how Git treats them: line endings, whether a file is text or binary, how to diff and merge it, and what to leave out of release archives.

Each line is a pattern followed by attributes:

*.sh        text eol=lf
*.bat       text eol=crlf
*.png       binary
docs/**     linguist-documentation
tests/      export-ignore

Line endings

Windows ends lines with CRLF (\r\n), macOS and Linux with LF (\n). When a team mixes systems, files flip between the two and every line shows up as changed in diffs. The robust fix is to declare it in the repository rather than rely on each person's core.autocrlf:

Attribute Meaning
* text=auto let Git detect text files and store them with LF in the repository
*.sh text eol=lf always LF in the working folder too (shell scripts break with CRLF)
*.bat text eol=crlf always CRLF in the working folder (Windows batch files)
*.png binary never touch line endings, never show a text diff

git ls-files --eol shows the ending stored in the index (i/), in the working folder (w/) and the attribute in effect:

bash · try it yourself
mkdir tools && cd tools && git init -q
printf 'echo one\r\necho two\r\n' > build.sh       # written on Windows: CRLF
printf '@echo off\r\n' > run.bat
git add . && git commit -q -m "Add scripts"
git ls-files --eol
echo "--- add .gitattributes and renormalize:"
printf '* text=auto\n*.sh text eol=lf\n*.bat text eol=crlf\n' > .gitattributes
git add --renormalize .
git add .gitattributes
git commit -q -m "Normalize line endings"
git ls-files --eol
echo "--- after a fresh checkout of the scripts:"
rm build.sh run.bat && git checkout -- build.sh run.bat
git ls-files --eol build.sh run.bat
i/crlf  w/crlf  attr/                 	build.sh
i/crlf  w/crlf  attr/                 	run.bat
--- add .gitattributes and renormalize:
i/lf    w/lf    attr/text=auto        	.gitattributes
i/lf    w/crlf  attr/text eol=lf      	build.sh
i/lf    w/crlf  attr/text eol=crlf    	run.bat
--- after a fresh checkout of the scripts:
i/lf    w/lf    attr/text eol=lf      	build.sh
i/lf    w/crlf  attr/text eol=crlf    	run.bat

After --renormalize, both scripts are stored with LF in the repository (i/lf), but the files on disk (w/) only change when Git writes them again. After a fresh checkout, build.sh has LF and run.bat CRLF, exactly as the attributes say.

Binary files and diffs

Git guesses whether a file is binary. Marking it explicitly avoids accidental text diffs and merges. You can also teach Git to diff a format as text with a textconv filter, here showing a binary-ish file through od:

bash · try it yourself
mkdir assets && cd assets && git init -q
printf 'logo.bin binary\n' > .gitattributes
printf '\x89PNG\x00\x01' > logo.bin
git add . && git commit -q -m "Add logo"
printf '\x89PNG\x00\x02' > logo.bin
git diff
echo "--- with a textconv driver:"
printf 'logo.bin diff=hex\n' > .gitattributes
git config diff.hex.textconv "od -An -tx1"
git diff -- logo.bin
diff --git a/logo.bin b/logo.bin
index f584f40..6bf43ff 100644
Binary files a/logo.bin and b/logo.bin differ
--- with a textconv driver:
diff --git a/logo.bin b/logo.bin
index f584f40..6bf43ff 100644
--- a/logo.bin
+++ b/logo.bin
@@ -1 +1 @@
- 89 50 4e 47 00 01
+ 89 50 4e 47 00 02

The same mechanism lets teams diff Word documents, images (by metadata) or encrypted files with the right external tool.

Checking attributes

git check-attr shows which attributes apply to a path, like check-ignore for ignores:

bash · try it yourself
mkdir site && cd site && git init -q
printf '* text=auto\n*.jpg binary\n/vendor/** linguist-vendored\ntests/ export-ignore\n' > .gitattributes
mkdir -p vendor/lib tests
touch vendor/lib/jquery.js photo.jpg index.html tests/run.sh
git check-attr -a photo.jpg vendor/lib/jquery.js index.html
photo.jpg: binary: set
photo.jpg: diff: unset
photo.jpg: merge: unset
photo.jpg: text: unset
vendor/lib/jquery.js: text: auto
vendor/lib/jquery.js: linguist-vendored: set
index.html: text: auto

binary is a macro for -diff -merge -text, which is why three attributes are listed for photo.jpg.

Leaving files out of archives

git archive builds a zip or tarball of a commit, for example a release download. Files marked export-ignore are skipped (GitHub's "Download ZIP" and release archives honour this):

bash · try it yourself
mkdir lib && cd lib && git init -q
mkdir tests
echo "def add(a, b): return a + b" > mathlib.py
echo "assert add(1, 2) == 3" > tests/test_mathlib.py
printf 'tests/ export-ignore\n.gitattributes export-ignore\n' > .gitattributes
git add . && git commit -q -m "Add mathlib"
git ls-files
echo "--- in the release archive:"
git archive --format=tar HEAD | tar -t
.gitattributes
mathlib.py
tests/test_mathlib.py
--- in the release archive:
mathlib.py

Other useful attributes

Attribute Effect
merge=ours on conflict, keep our version of the file (needs git config merge.ours.driver true)
filter=lfs store the file with Git LFS (next lesson)
linguist-generated, linguist-vendored GitHub hides the file in PR diffs / language stats
diff=python, diff=java, … better hunk headers (function names) in diffs
whitespace=… rules for git diff --check

Test yourself

Which line forces shell scripts to always have LF endings in the working folder?

  1. *.sh binary
  2. *.sh text eol=lf
  3. *.sh eol=crlf
  4. *.sh -diff

What does binary in .gitattributes mean?

  1. Compress the file
  2. -diff -merge -text: no text diff, no text merge, no line-ending conversion
  3. Store the file in LFS
  4. Ignore the file

What does export-ignore affect?

  1. git push
  2. git archive (release tarballs/zips)
  3. git status
  4. git clone