Sub topic 5
Forcing working-tree endings with eol
This chapter assumes you know what LF and CRLF are, have seen text=auto in a .gitattributes file, and can run Git commands in a terminal.
What eol does
The text attribute tells Git to normalize a file's line endings to LF in the repository. It does not say which endings you want in your working tree. Without further instructions, Git picks the working-tree style from the platform default, or from core.eol and core.autocrlf. Those are per-user settings, so two developers can check out the same file with different endings.
The eol attribute fixes the working-tree style for matching files in the repository itself. It takes two values:
| Setting | Repository (index) | Working tree |
|---|---|---|
| `eol=lf` | LF | LF on every platform |
| `eol=crlf` | LF | CRLF on every platform |
The repository copy is LF in both cases. Only the checked-out copy changes.
Writing eol rules
Some files break if they have the wrong ending. A shell script with CRLF fails on Linux and macOS. A Windows batch file with bare LF can misbehave in cmd.exe. Pin those files explicitly:
# Let Git detect text files and normalize them to LF in the repository
* text=auto
# Shell scripts must keep LF in the working tree on every platform
*.sh text eol=lf
# Windows batch files must keep CRLF in the working tree
*.bat text eol=crlf
*.cmd text eol=crlfGit applies the last matching line for each attribute. *.sh matches after * here, so it overrides text=auto for .sh files. The eol line is also the first rule that mentions eol for those files, so nothing else competes with it.
Writing text eol=lf together is the clearest form. Git treats eol as implying text when text is unset. Spelling both out makes the intent obvious to the next reader.
Interaction with other settings
eol takes priority over core.autocrlf and core.eol for the paths it matches. This is why a committed .gitattributes is more reliable than a per-user setting. A Windows developer with core.autocrlf=true still gets LF in deploy.sh, because the file-level rule wins.
eol only applies when Git considers the file text. It is ignored in these cases:
- The file has
-textorbinaryset. - The file has
text=autoand Git detects it as binary, for example because it contains NUL bytes.
# eol is ignored here because -text disables conversion
*.dat -text eol=crlfIn that example the eol=crlf setting does nothing. Check for this kind of conflict when a rule seems not to work.
Checking the result
After you add rules, confirm that Git resolves them as you expect:
# Show every attribute that applies to one path
git check-attr -a -- scripts/deploy.sh
# Show the line-ending state of every tracked file
git ls-files --eolFor scripts/deploy.sh with the rules above, git check-attr should report text: set and eol: lf. In the git ls-files --eol output you want i/lf w/lf attr/text eol=lf. Here i/ is the index, w/ is the working tree, and attr/ lists the attributes in effect.
Existing files do not change on their own
Adding an eol rule does not rewrite files already checked out. Git applies conversion when it writes a file to the working tree or stages a file. If deploy.sh is already in your working tree with CRLF, you will see w/crlf in git ls-files --eol until you refresh it. Renormalizing the repository, covered in a later chapter, handles the stored copies. Refreshing the working tree is a separate step.
Common mistakes
- Setting
eol=crlfon a file Git sees as binary. The rule is silently skipped. - Forcing
eol=crlfon files that need LF, such asMakefileorDockerfile. Tools that run on Linux will fail on the extra carriage return. - Relying on
core.autocrlffor scripts. It varies by developer, so one person's checkout still breaks. - Putting a broad
eolrule after a specific one. The later rule wins for the same attribute.
Exercise
A Windows developer has core.autocrlf=true. Your repository contains install.sh and a .gitattributes with the line "*.sh text eol=lf". What line endings does install.sh have in their working tree, and why?
Show answer
LF. The eol attribute in .gitattributes takes priority over core.autocrlf and core.eol for matching paths, so the file is checked out with LF even on Windows.
Exercise
You add "*.dat -text eol=crlf" to .gitattributes. Will .dat files be converted to CRLF on checkout?
Show answer
No. -text turns off text handling, and eol only applies to files treated as text, so the eol setting is ignored.