Force LF Line Endings with eol=lf
Configure .gitattributes to enforce Unix-style LF line endings for specific file types, preventing CRLF corruption in shell scripts and configuration files.
Detailed Explanation
Forcing LF with eol=lf
While text=auto normalizes line endings in the repository, it still converts to CRLF on Windows during checkout. Some files must always use LF regardless of the developer's platform — shell scripts, Makefiles, and certain configuration files will break with CRLF endings.
The Directive
*.sh text eol=lf
*.bash text eol=lf
Makefile text eol=lf
*.yml text eol=lf
*.yaml text eol=lf
The text eol=lf combination does two things:
text— marks the file as a text file for normalizationeol=lf— forces LF endings even in the working directory on Windows
Why Shell Scripts Need LF
Unix shells (bash, sh, zsh) interpret \r (carriage return) as part of the command, leading to errors like:
/bin/bash^M: bad interpreter: No such file or directory
The ^M is the carriage return character from CRLF. By forcing eol=lf, Windows developers will always get LF endings for these files, preventing this class of bugs.
Files That Commonly Require LF
| File type | Why |
|---|---|
*.sh, *.bash |
Shell interpreters require LF |
Makefile |
Make requires LF for recipe lines |
*.yml, *.yaml |
Some YAML parsers are LF-sensitive |
.env |
Docker and dotenv may fail with CRLF |
Dockerfile |
Docker build can fail with CRLF |
.editorconfig |
Convention for editor configs |
Combining with text=auto
* text=auto
*.sh text eol=lf
*.bat text eol=crlf
The more specific rules override the global text=auto for matching file patterns.
Use Case
Any repository containing shell scripts, Makefiles, Dockerfiles, or CI configuration files should use `eol=lf` for those file types. This is especially critical for projects where Windows developers contribute to Unix-targeted deployment scripts.