Sub topic 10
Handling line endings across operating systems
This chapter assumes you have Git installed, can use a terminal, and know what a repository and a commit are. You should also know how to run git config at the global scope, which earlier chapters covered.
Why line endings matter
A text file marks the end of each line with invisible characters. Windows uses two: carriage return and line feed, written CRLF. Linux and macOS use one: line feed, written LF.
When a team mixes systems, the same file can differ only in its line endings. Git then reports every line as changed, and diffs become unreadable. Some tools also fail on the wrong ending. For example, a shell script with CRLF endings can break on Linux with an error about a bad interpreter.
The core.autocrlf setting tells Git how to convert endings when it moves files between your working directory and the repository.
The three values
| Value | On commit | On checkout | Typical user |
|---|---|---|---|
| `true` | CRLF becomes LF | LF becomes CRLF | Windows |
| `input` | CRLF becomes LF | No change | Linux and macOS |
| `false` | No change | No change | Teams that manage endings another way |
The goal is the same in each case: the repository stores LF, and your working files use the style your system expects.
Setting it
On Linux and macOS, use input:
git config --global core.autocrlf inputThis converts CRLF to LF on commit and leaves files alone on checkout. It protects the repository if you accidentally commit a file that has CRLF endings, for example one copied from a Windows machine.
On Windows, use true:
git config --global core.autocrlf trueCheck the effective value and where it comes from:
git config --show-origin --get core.autocrlfThe output shows the file path first, then the value. If the value is not what you set, a more specific scope may be overriding it. The precedence chapter explains how scopes override each other.
Seeing the effect
Suppose you create a file with CRLF endings on a machine set to input:
printf 'hello\r\nworld\r\n' > demo.txt
git add demo.txtGit may print a warning that CRLF will be replaced by LF. The staged content now has LF endings, while the file in your working directory is unchanged. You can confirm this with git ls-files --eol, which shows the endings in the index and in the working tree.
git ls-files --eol demo.txtThe output lists i/lf for the index and w/crlf for the working tree.
Common problems
- Every line shows as modified after cloning. The repository contains CRLF files, and your setting converts them. Run
git ls-files --eolto see which files have mixed endings. - A script fails on Linux with a bad interpreter error. The file was committed with CRLF. Set
core.autocrlftoinputand re-commit the file with LF endings. - Different team members use different settings. Per-user configuration is easy to get wrong. A
.gitattributesfile committed to the repository, for example with the line* text=auto, applies the same rule to everyone and takes the decision out of individual setups.
Which value should you choose?
Choose by operating system. Use true on Windows and input on Linux and macOS. Use false only when the repository's .gitattributes file already controls endings, or when you have a specific reason to keep files byte for byte.
Exercise
You are on macOS and a colleague on Windows sends you a file with CRLF endings. You want Git to store it with LF endings without changing the file in your working directory. Which command do you run, and which value does it set?
Show answer
Run git config --global core.autocrlf input. The value input converts CRLF to LF when you commit and does not change files on checkout, so your working copy stays as it was.
Exercise
After setting core.autocrlf to input, you still see CRLF behavior. Which command shows which file supplies the effective value?
Show answer
Run git config --show-origin --get core.autocrlf. It prints the file path and the value, so you can see whether a local or system file is overriding your global setting.