In this lesson, you’ll learn how to configure Git to automatically manage line ending differences so your code stays consistent regardless of whether you're working on Windows, macOS, or Linux.
Why Line Endings Matter
Different operating systems use invisible characters to mark the end of a line in a text file. Windows uses a combination of two characters (Carriage Return and Line Feed, or CRLF), while macOS and Linux use only one (LF).
If you work on a team where one person uses Windows and another uses macOS, Git might get confused. It might think every single line in your file has changed—simply because the invisible line ending characters are different. This can clutter your project history and make it difficult to see actual code changes.
Configuring Git for Your OS
To avoid this headache, you can tell Git to handle these conversions for you automatically. Git will "normalize" the files when you save them to the repository (converting them to LF) and then convert them back to the appropriate format for your specific operating system when you check them out.
If you are using Windows, run this command in your terminal:
git config --global core.autocrlf true
This tells Git to convert LF to CRLF when you check out files, and convert them back to LF when you commit.
If you are using macOS or Linux, use this command:
git config --global core.autocrlf input
This tells Git to leave files as they are on checkout but convert them to LF when you commit, ensuring a uniform standard in your shared repository.
By setting these configurations, you allow Git to act as a silent translator, ensuring that your collaborative workflow remains focused on the code itself rather than invisible system formatting differences. This setup is a vital step in maintaining a clean "Source of Truth" for your project, as discussed in our earlier lessons.
Key Takeaway: Configuring core.autocrlf ensures that line endings are normalized automatically, preventing unnecessary "ghost" changes in your version history when working across different operating systems.