0
0
GitHow-ToBeginner · 3 min read

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.autocrlf incorrectly for your OS, causing unwanted line ending changes.
  • Not committing a .gitattributes file, 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

SettingDescriptionRecommended Use
core.autocrlf=trueConvert LF to CRLF on checkout, CRLF to LF on commitWindows systems
core.autocrlf=inputConvert CRLF to LF on commit, no conversion on checkoutUnix/Linux/macOS systems
core.autocrlf=falseNo automatic conversionIf you manage line endings manually
.gitattributesDefine line endings per file typeCross-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.