Sub topic 2
Per-user settings: core.autocrlf and its limits
This chapter assumes you know the basic Git workflow (clone, add, commit, status, diff) and can use a terminal. It also assumes you know the difference between LF and CRLF line endings from the previous chapter. Here you will learn what core.autocrlf does, which value to pick, and why it can't be your only defense.
What core.autocrlf does
core.autocrlf is a Git configuration setting that converts line endings when files move between your working tree and the repository. Git runs the conversion at two moments:
- When you stage a file (
git add), Git can turn CRLF into LF before storing it. - When Git writes a file to disk (
git checkout,git clone), it can turn LF into CRLF.
The setting has three values:
| Value | On add or commit | On checkout | Typical user |
|---|---|---|---|
| `true` | CRLF becomes LF | LF becomes CRLF | Windows |
| `input` | CRLF becomes LF | No conversion | macOS and Linux |
| `false` | No conversion | No conversion | Anyone who relies on `.gitattributes` |
The goal is a repository that stores only LF, while each developer sees the endings their platform expects.
Setting the value
You set it per user, usually with --global:
# macOS and Linux: convert CRLF to LF on commit, leave the working tree alone
git config --global core.autocrlf input
# Windows: commit as LF, check out as CRLF
git config --global core.autocrlf trueYou can read the effective value, and the file it came from, with git config --show-origin core.autocrlf.
Related settings
Two other settings change how the conversion behaves:
core.eolchooses the ending used in the working tree when Git converts a file. Its values arelf,crlfandnative. The default,native, means CRLF on Windows and LF elsewhere.core.safecrlfguards against conversions that can't be reversed. Set it towarnto get a message, or totrueto make Git refuse the operation. This helps when a file contains mixed endings.
git config --global core.safecrlf warnHow Git decides a file is text
With core.autocrlf set to true or input, Git inspects the content of each file. If it finds no NUL bytes and the line-ending pattern looks consistent, Git treats the file as text and converts it. This is a heuristic, so a guess can be wrong.
The limits of core.autocrlf
The setting is useful, but it has real weaknesses.
- It isn't shared. It lives in your own config, not in the repository. A new teammate, a fresh laptop or a CI runner starts with the default (
false), and nobody is warned. - It applies one rule to every file. On Windows with
true, a shell script is checked out with CRLF. The shebang line then becomes#!/bin/shfollowed by a carriage return, and the script fails on Linux or inside a container. A.batfile needs the opposite treatment, andcore.autocrlfcan't express that. - It relies on guessing. A file that Git misclassifies as text can be corrupted. Known binary types should be marked explicitly.
- Different users get different results. Two developers with different settings can commit the same file with different endings, which brings back noisy diffs.
Where .gitattributes fits
A .gitattributes file is committed with the repository, so every clone gets the same rules. When a path has the text or eol attribute, that attribute takes precedence over core.autocrlf for that path. The usual setup is:
- Keep
core.autocrlfas a convenient default for files the repository doesn't mention. - Put the real policy in
.gitattributes, which the next chapters cover.
You can check what a path will do with git ls-files --eol, which shows the endings in the index and in the working tree.
git ls-files --eolA typical line looks like i/lf w/crlf attr/text=auto eol=lf scripts/deploy.sh. Here the index holds LF, the working tree holds CRLF, and the attribute column shows the rule in effect.
Choosing a value
- On Windows, use
trueif the repository has no.gitattributesrules. Usefalseif it has them. - On macOS and Linux, use
input. It protects you if a CRLF file arrives from a Windows teammate or an editor. - Avoid changing the setting on a repository with existing history without a plan. Files that were committed with CRLF will then appear as modified. The renormalization chapter explains how to handle that.
Exercise
A Windows developer has core.autocrlf=true and checks out a repository with no .gitattributes file. A deploy.sh script fails on a Linux server with an error about "\r". What caused it, and why can't core.autocrlf fix it?
Show answer
With autocrlf=true, Git converted the script's LF endings to CRLF on checkout, and the script was then copied to Linux with those endings. core.autocrlf applies one rule to all text files and is stored in per-user configuration, so it can't say that this one file must keep LF. A committed .gitattributes line such as "*.sh text eol=lf" can.