Line Endings and .gitattributes: autocrlf, eol, Binary Files and Diff Drivers
End line-ending trouble between Windows, macOS and Linux with .gitattributes: text=auto, eol=lf and crlf, core.autocrlf and why attributes beat it, renormalizing a repository, binary files, diff drivers and textconv, export-ignore, merge strategies per file and checking attributes.
kvi5hnuCode Sprout team member8 min readOn this page
Windows ends lines of text with two characters, carriage return and line feed (CRLF, \r\n). Linux and macOS use only the line feed (LF, \n). When a team mixes systems, files flip between the two, and suddenly every line of a file shows as changed, shell scripts fail with bad interpreter: /bin/sh^M, and diffs become unreadable. Git can normalize line endings: store LF in the repository and convert on checkout where needed. The reliable way to set that up is a .gitattributes file, which is also how you tell Git which files are binary, how to diff them, and more.
Seeing what you have
git ls-files --eol shows, for each tracked file, the line endings in the index (i/) and working tree (w/), and the attributes that apply:
git init -q trail-guide && cd trail-guide
printf 'Lake loop\r\nRidge loop\r\n' > trails.txt # written on Windows
printf '#!/bin/sh\necho hello\n' > run.sh # written on Linux
git add .; git commit -q -m "Add files"
echo "=== before: CRLF is stored in the repository"
git ls-files --eol
echo "* text=auto" > .gitattributes
git add .gitattributes
git add --renormalize . # re-store tracked files under the new rules
git commit -q -m "Normalize line endings"
echo "=== after: LF in the repository"
git ls-files --eol
=== before: CRLF is stored in the repository
i/lf w/lf attr/ run.sh
i/crlf w/crlf attr/ trails.txt
=== after: LF in the repository
i/lf w/lf attr/text=auto .gitattributes
i/lf w/lf attr/text=auto run.sh
i/lf w/crlf attr/text=auto trails.txt
i/crlfmeans the repository stores CRLF, the situation to fix. After normalizing, the index hasi/lffor every text file.w/crlffortrails.txtis the file still on disk. That's fine: Git converts it the next time it writes the file, such as on checkout. When you stage a file in that state, Git prints a warning like "CRLF will be replaced by LF the next time Git touches it".git add --renormalize .re-adds every tracked file using the current attributes. Commit the result in its own commit, named so, and consider adding it to.git-blame-ignore-revs(article 30).
.gitattributes
.gitattributes is a text file, committed to the repository, with lines of pattern followed by attributes. Patterns work like .gitignore patterns (article 8), and later lines override earlier ones. Comments must be on lines of their own: a # after a rule is read as another attribute name, which makes Git ignore the whole line:
# .gitattributes
# Git decides text or binary. Text is stored with LF.
* text=auto
# Always LF on disk, even on Windows, and always CRLF, even on Linux
*.sh text eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf
# Never convert, never diff or merge as text
*.png binary
*.jpg binary
*.pdf binary
| Attribute | Meaning |
|---|---|
text=auto | Git detects text files and normalizes them to LF in the repository. Binary files are left alone |
text | always treat as text, and normalize |
-text | never convert |
eol=lf / eol=crlf | treat as text, and use this ending in the working tree on every system |
binary | shorthand for -text -diff -merge: no conversion, no textual diff or merge |
Because the file is committed, everyone gets the same rules, whatever their own settings. That's the key advantage over the configuration option below.
git init -q trail-guide && cd trail-guide
printf '* text=auto\n*.sh text eol=lf\n*.bat text eol=crlf\n*.png binary\n' > .gitattributes
printf 'echo hi\n' > run.sh
printf 'echo hi\n' > run.bat
printf '\x89PNG\r\n\x1a\n\x00\x01' > logo.png
git add . 2> /dev/null; git commit -q -m "Add files"
rm run.sh run.bat; git checkout -q -- . # files rewritten using the attributes
git ls-files --eol run.sh run.bat logo.png
echo "=== which attributes apply?"
git check-attr -a run.bat logo.png
echo "=== a binary change"
printf '\x89PNG\r\n\x1a\n\x00\x02' > logo.png
git diff logo.png | tail -1
i/-text w/-text attr/-text logo.png
i/lf w/crlf attr/text eol=crlf run.bat
i/lf w/lf attr/text eol=lf run.sh
=== which attributes apply?
run.bat: text: set
run.bat: eol: crlf
logo.png: binary: set
logo.png: diff: unset
logo.png: merge: unset
logo.png: text: unset
=== a binary change
Binary files a/logo.png and b/logo.png differ
run.bat is stored with LF but written with CRLF, run.sh keeps LF everywhere, and logo.png is never touched. git check-attr -a <file> lists the attributes that apply, which is the way to debug a .gitattributes file.
core.autocrlf
Before .gitattributes was common, line endings were controlled per user with core.autocrlf:
| Value | On commit | On checkout | Typical use |
|---|---|---|---|
true | CRLF to LF | LF to CRLF | Windows |
input | CRLF to LF | no conversion | macOS and Linux |
false | no conversion | no conversion | the default on most systems |
Git for Windows' installer usually sets core.autocrlf=true. The problem is that it's a personal setting: one developer with it set wrongly commits CRLF files, and everyone else sees changed lines. A committed .gitattributes with * text=auto makes Git normalize text files to LF in the repository whatever anyone's core.autocrlf says, and eol= rules also fix the working-tree endings. Without eol=, the endings on disk still follow core.autocrlf or core.eol. So put line-ending rules in .gitattributes and treat autocrlf as a personal preference for the working tree.
Related settings:
core.eolsets the ending used fortextfiles without aneolattribute:native(the default),lforcrlf.core.safecrlfwarns, or withtruerefuses, when a conversion wouldn't round-trip exactly, as with files containing mixed endings.
Fixing a repository
A one-time cleanup for a project with mixed endings:
- Agree on the rules, and commit a
.gitattributes, starting with* text=autoand addingeol=andbinarylines for specific types. - Make sure everyone has committed their work.
- Run
git add --renormalize .and commit it as "Normalize line endings". - Everyone pulls. Files in working trees are converted the next time Git writes them. To refresh everything immediately:
git rm --cached -r . -q && git reset --hard, with no uncommitted work.
Editors help too: most respect .editorconfig files, whose end_of_line = lf setting controls what the editor writes.
Diff drivers and textconv
Attributes also control how files are diffed. A diff driver named in .gitattributes and defined in configuration can:
- improve hunk headers for a language: Git has built-in drivers such as
python,java,csharp,rust,go,markdownandhtml, which make-p,-Wandblame -L :funcrecognize functions (articles 7, 30 and 32); - turn binary files into text for diffing, with
textconv.
git init -q trail-guide && cd trail-guide
echo "*.bin diff=hex" > .gitattributes
git config diff.hex.textconv "xxd" # how to turn a .bin file into text
printf 'TRAIL\x00\x05\x00\x08' > data.bin; git add .; git commit -q -m "Add data"
printf 'TRAIL\x00\x05\x00\x09' > data.bin
git diff data.bin | tail -3
@@ -1 +1 @@
-00000000: 5452 4149 4c00 0500 08 TRAIL....
+00000000: 5452 4149 4c00 0500 09 TRAIL....
The attribute, diff=hex, is in the committed .gitattributes. The driver's command is in configuration, because Git never runs commands from repository files automatically, for security. Each developer adds the diff.<name>.textconv setting once. Common textconv tools convert PDFs, Word documents, images' metadata (exiftool) or SQLite databases to text.
Similar attribute pairs exist for merging: merge=<driver> with merge.<driver>.driver in configuration, and the built-in merge=union, which keeps lines from both sides, as for changelog files where every side adds lines. -merge makes Git always report a conflict, keeping the current version.
Other useful attributes
| Attribute | Effect |
|---|---|
export-ignore | leave the path out of git archive output, such as tests or CI files in release tarballs (article 38) |
export-subst | expand placeholders like $Format:%H$ in archived files |
filter=lfs diff=lfs merge=lfs -text | store the file with Git LFS (article 43) |
whitespace=... | per-file whitespace rules for git diff --check |
working-tree-encoding=UTF-16LE-BOM | keep a file in UTF-16 on disk but store it as UTF-8, so diffs work |
linguist-generated, linguist-vendored | GitHub-specific: hide generated or vendored files in diffs and language statistics |
Attributes for your own repository only, not shared, go in .git/info/attributes. Personal attributes for all repositories go in the file named by core.attributesFile, by default ~/.config/git/attributes.
What's current
- A committed
.gitattributeswith* text=autois the standard approach, and most project templates include one. git ls-files --eol(Git 2.8) andgit add --renormalize(Git 2.16) are the tools for checking and fixing endings.- Git for Windows still defaults to
core.autocrlf=truein its installer, so attributes are what make a team consistent.
Common questions
Why does every line of a file show as changed, though I edited one?
The line endings changed: your editor saved the file with CRLF instead of LF, or the reverse. git diff --ignore-cr-at-eol (or -w) confirms it. Add .gitattributes rules, renormalize once, and set your editor to the project's ending.
My shell script fails with /bin/sh^M: bad interpreter. Why?
It was checked out with CRLF endings, and the ^M is the carriage return. Add *.sh text eol=lf to .gitattributes, then check what the repository stores with git ls-files --eol run.sh. If it shows i/crlf, run git add --renormalize run.sh and commit. Then remove the file and check it out again: rm run.sh && git checkout run.sh.
What does "warning: in the working copy of 'x', LF will be replaced by CRLF the next time Git touches it" mean?
Git noticed the file on disk doesn't match the ending its rules call for, and will convert it the next time it writes the file, such as on checkout. It's informational. To convert now, delete the file and check it out again, because a plain git checkout <file> skips files it considers unchanged.
Should I set core.autocrlf?
In a repository with a .gitattributes covering line endings, it doesn't matter much. Elsewhere, true on Windows and input on macOS and Linux are the usual choices. The best fix is adding .gitattributes to the projects you work on.
Summary
- Windows uses CRLF and other systems LF. Git can store LF in the repository and convert on checkout.
- Commit a
.gitattributeswith* text=auto, pluseol=lforeol=crlffor files that need one ending everywhere, andbinaryfor binary types. git ls-files --eolshows endings,git check-attr -ashows attributes, andgit add --renormalize .fixes a repository.core.autocrlfis a personal fallback:trueon Windows,inputelsewhere. Attributes win.diff=<driver>withdiff.<driver>.textconvmakes binary files diffable, and built-in drivers improve hunk headers.export-ignore,merge=union,working-tree-encodingandlinguist-*handle other per-file needs.
Practice questions
A Windows teammate keeps committing files with CRLF, and Linux users see every line changed. What's the lasting fix?
Answer
Commit a .gitattributes with * text=auto (and eol= rules where needed), then run git add --renormalize . and commit the normalization. The attributes apply to everyone regardless of their core.autocrlf, so CRLF is converted to LF when they commit from then on.
Which .gitattributes line makes batch files always use CRLF on disk, even on macOS?
Answer
*.bat text eol=crlf. eol=crlf writes CRLF in the working tree on every system, while the repository still stores LF.
You want readable diffs of changes to .sqlite database files. How do you set it up?
Answer
In .gitattributes, commit *.sqlite diff=sqlite. Git runs the textconv command with the file's path appended, so write a small script, such as ~/bin/sqlite-dump containing sqlite3 "$1" .dump, make it executable, and have each developer configure git config --global diff.sqlite.textconv ~/bin/sqlite-dump. Git runs it on both versions and diffs the text output. The command lives in configuration because Git won't run commands from repository files.
Previous: Git Aliases: Shortcuts, Shell Aliases and a Useful Collection
Next: Git Hooks: pre-commit, commit-msg, pre-push, Server-Side Hooks and Sharing Hooks

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.