0
0
GitComparisonBeginner · 3 min read

CRLF vs LF in Git: Key Differences and When to Use Each

In Git, LF (Line Feed) is the standard line ending used on Unix/Linux/macOS systems, while CRLF (Carriage Return + Line Feed) is used on Windows. Git can automatically convert between these to keep files consistent across platforms using the core.autocrlf setting.
⚖️

Quick Comparison

This table summarizes the main differences between CRLF and LF line endings in Git.

AspectLF (Line Feed)CRLF (Carriage Return + Line Feed)
Used byUnix, Linux, macOSWindows
Line ending charactersOne character: \nTwo characters: \r\n
Default Git behaviorStored as-is in reposConverted to LF in repos by default
Common issuesNo issues on Unix systemsCan cause problems on Unix if not converted
Git config settingcore.autocrlf=input recommended on Unixcore.autocrlf=true recommended on Windows
File size impactSlightly smaller filesSlightly larger files due to extra \r
⚖️

Key Differences

LF stands for Line Feed and is a single character (\n) used to mark the end of a line in text files on Unix-like systems such as Linux and macOS. CRLF stands for Carriage Return + Line Feed and uses two characters (\r\n) to mark line endings on Windows systems.

Git stores files internally with LF line endings by default to maintain consistency across platforms. When working on Windows, Git can convert CRLF line endings to LF on commit and convert back to CRLF on checkout using the core.autocrlf setting. This prevents issues like broken scripts or diff noise caused by inconsistent line endings.

Choosing the right line ending and Git configuration depends on your team's operating systems and tools. Using consistent line endings avoids merge conflicts and unexpected behavior in code editors or build tools.

💻

LF Example in Git

bash
echo -e "line1\nline2" > file.txt
cat -A file.txt
Output
line1$ line2$
↔️

CRLF Equivalent in Git

bash
echo -e "line1\r\nline2" > file.txt
cat -A file.txt
Output
line1^M$ line2$
🎯

When to Use Which

Choose LF when working primarily on Unix-like systems or when your team uses cross-platform tools that expect Unix-style line endings. Set core.autocrlf=input on these systems to convert CRLF to LF on commit but leave LF unchanged on checkout.

Choose CRLF when working mainly on Windows with tools that require Windows-style line endings. Set core.autocrlf=true to convert LF to CRLF on checkout and CRLF to LF on commit, keeping the repository consistent.

For mixed teams, consider using .gitattributes files to enforce line endings per file type and avoid conflicts.

Key Takeaways

Git stores files with LF line endings internally for consistency across platforms.
Windows uses CRLF line endings; Unix-like systems use LF.
Use core.autocrlf=true on Windows to auto-convert line endings.
Use core.autocrlf=input on Unix to convert CRLF to LF on commit only.
Use .gitattributes to enforce line endings in mixed environments.