How do I force Git to use LF instead of CRLF under Windows?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with Git on Windows, developers often encounter issues related to line endings, specifically the difference between LF (Line Feed) and CR+LF (Carriage Return+Line Feed). This stems from the fact that Unix-based systems, like Linux and macOS, use LF as a line ending, while Windows OS uses CR+LF. Mismanaged line endings can lead to issues when collaborating across different systems. This article provides a comprehensive guide on forcing Git to use LF instead of CR+LF under Windows.
Understanding Line Endings
What are LF and CR+LF?
• LF (Line Feed): Denoted as \n
, it is the standard line ending used by Unix-based systems.
• CR+LF (Carriage Return + Line Feed): Denoted as \r\n
, it is typically used by Windows systems.
Differences in line endings can cause problems in a collaborative project, especially if changes are made on different operating systems without consistent treatment of line endings.
Git's Approach to Handling Line Endings
Git provides several options for handling line endings. These allow developers to collaborate seamlessly regardless of their operating systems.
Configuring Git to Use LF
Git Configuration Options
Git has a core.autocrlf
setting that controls how line endings are handled:
• true
: Convert LF to CR+LF on checkout and CR+LF to LF on commit. Useful when working in Windows and collaborating with Unix-based systems.
• input
: Convert CR+LF to LF on commit but do not change line endings on checkout. This setting is typically used when developers want to maintain LF line endings across all systems.
• false
: Do not change line endings. Use this when you want to manage line endings manually or when collaborating with others who use consistent line endings.
Setting Git to Use LF
To force Git to use LF line endings, follow these steps using Git's configuration:
- Open a terminal or command prompt.
- To set the line ending conversion to
inputmode, use the following command:
• text=auto
• Editor Configuration: Ensure your text editor is configured to save files with LF line endings, which prevents accidental introduction of CR+LF endings. • Global vs Local Configurations: Use global configurations to apply settings for all repositories, while local configurations offer the flexibility to tweak settings per project. • Collaborative Guidelines: Establish team conventions around line endings to maintain consistency across the project and reduce merge conflicts.

