How to Configure Line Endings in Git for Cross-Platform Consistency
To configure line endings in Git, use the
core.autocrlf setting to control automatic conversion between Windows (CRLF) and Unix (LF) styles. Alternatively, use a .gitattributes file to specify line ending rules per file type for consistent behavior across platforms.Syntax
The main ways to configure line endings in Git are:
- core.autocrlf: Controls automatic line ending conversion on commit and checkout.
- .gitattributes: Defines line ending rules per file or pattern.
Values for core.autocrlf:
true: Convert LF to CRLF on checkout, CRLF to LF on commit (Windows recommended).input: Convert CRLF to LF on commit, no conversion on checkout (Unix recommended).false: No conversion (use if you want to manage line endings yourself).
bash
git config --global core.autocrlf <true|input|false> # Example .gitattributes content: *.sh text eol=lf *.bat text eol=crlf *.txt text
Example
This example sets Git to convert line endings automatically on Windows and uses a .gitattributes file to enforce LF endings for shell scripts and CRLF for batch files.
bash
git config --global core.autocrlf true echo "*.sh text eol=lf" > .gitattributes echo "*.bat text eol=crlf" >> .gitattributes echo "*.txt text" >> .gitattributes # Check the config git config --global core.autocrlf
Output
true
Common Pitfalls
Common mistakes when configuring line endings in Git include:
- Setting
core.autocrlfincorrectly for your OS, causing unwanted line ending changes. - Not committing a
.gitattributesfile, leading to inconsistent line endings across team members. - Mixing manual line ending changes with Git's automatic conversion, causing conflicts.
Always choose one method and apply it consistently.
bash
git config --global core.autocrlf false # Wrong on Windows if you want automatic conversion # Correct for Windows: git config --global core.autocrlf true
Quick Reference
| Setting | Description | Recommended Use |
|---|---|---|
| core.autocrlf=true | Convert LF to CRLF on checkout, CRLF to LF on commit | Windows systems |
| core.autocrlf=input | Convert CRLF to LF on commit, no conversion on checkout | Unix/Linux/macOS systems |
| core.autocrlf=false | No automatic conversion | If you manage line endings manually |
| .gitattributes | Define line endings per file type | Cross-platform projects needing precise control |
Key Takeaways
Use core.autocrlf=true on Windows to handle line endings automatically.
Use core.autocrlf=input on Unix-like systems to keep LF endings.
Add a .gitattributes file to enforce consistent line endings per file type.
Avoid mixing manual line ending changes with Git's automatic conversion.
Commit your line ending settings to share them with your team.