CRLF vs LF in Git: Key Differences and When to Use Each
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.
| Aspect | LF (Line Feed) | CRLF (Carriage Return + Line Feed) |
|---|---|---|
| Used by | Unix, Linux, macOS | Windows |
| Line ending characters | One character: \n | Two characters: \r\n |
| Default Git behavior | Stored as-is in repos | Converted to LF in repos by default |
| Common issues | No issues on Unix systems | Can cause problems on Unix if not converted |
| Git config setting | core.autocrlf=input recommended on Unix | core.autocrlf=true recommended on Windows |
| File size impact | Slightly smaller files | Slightly 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
echo -e "line1\nline2" > file.txt
cat -A file.txtCRLF Equivalent in Git
echo -e "line1\r\nline2" > file.txt
cat -A file.txtWhen 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.