Git line endings: .gitattributes text and eol vs core.autocrlf
What to check when git diff shows ^M, based on Git's own documentation: what core.autocrlf and the text and eol attributes each decide, and how to fix files already committed with CRLF.
Run git diff on a file edited on Windows and you may see ^M at the end of newly added lines. The Git FAQ explains that Git expects Unix line endings (LF) by default. It treats the CR in a Windows line ending (CRLF) as trailing whitespace and displays it as ^M. By default, trailing whitespace is shown only on new lines.
Two places control line endings, core.autocrlf and .gitattributes, and they do different jobs.
Where conversion happens
Line-ending conversion applies only to files treated as text. Adding a file to the index normalizes its line endings to LF. Checking it out may turn LF into CRLF, depending on the configuration.
The text attribute decides whether a file is text. When no attribute is set, core.autocrlf decides.
core.autocrlf
core.autocrlf=true is equivalent to setting text=auto on all files and setting core.eol to crlf. With input, Git does not convert line endings on checkout.
The gitattributes documentation presents this setting as an option for users who want CRLF in their working directory regardless of the repository. It normalizes text files they add to LF, but it does not force normalization across the repository. To apply the rule to files added by every contributor, set the text attribute in the repository.
.gitattributes
A committed .gitattributes file is shared with the repository. Each clone’s $GIT_DIR/info/attributes has higher precedence, however, so a local configuration can differ.
* text=auto
*.sh text eol=lf
*.bat text eol=crlf
*.jpg -text
text=auto: Git examines the content and decides whether the file is text.text: Git treats the file as text.-text: Git does not attempt line-ending conversion.eol=lf,eol=crlf: these fix the line ending used in the working directory.
The eol attribute has an effect when text or text=auto is set. If text was left unspecified, setting eol also sets text automatically.
A text file with no eol follows core.autocrlf or core.eol. If neither is set, checkout uses CRLF on Windows and LF elsewhere. core.eol defaults to native and is ignored when core.autocrlf is true or input.
The Git FAQ recommends storing text files with LF and marking text and binary files explicitly in .gitattributes. Its examples require LF for shell scripts and CRLF for batch files.
Files already committed with CRLF
text=auto does not convert a file already stored in the index with CRLF. Adding .gitattributes alone therefore leaves existing files unchanged.
Git documents this procedure: start with a clean working directory, create .gitattributes, run the following command, inspect the affected files with git status, and then commit them.
git add --renormalize .
The option leaves lone CR characters untouched.
You can inspect the current state with git ls-files --eol. This line was produced in the Motitory site repository with Git 2.39.5 (Apple Git-154):
i/lf w/lf attr/text=auto eol=lf .gitattributes
i/ is the line ending in the index, w/ is the line ending in the working directory, and attr/ is the attribute in effect.
Caveats
The git-config documentation says CRLF conversion bears a slight chance of corrupting data. A file that mixed LF and CRLF cannot be recreated in its original form after the commit. A binary file wrongly classified as text can also be corrupted, so mark such files with -text.
With core.safecrlf=true, Git rejects an irreversible conversion. With warn, it only prints a warning.
This article follows the Git 2.56.0 documentation. It does not cover editor settings or the choices offered by the Git for Windows installer.
Documentation used
All of these are the Git project’s own documentation. Line-ending conversion is Git’s implementation behavior, not an independent standard.
- gitattributes:
text,eol, line-ending conversion and the precedence of attribute files. - git-config: the Git 2.56.0 configuration documentation.
- gitfaq:
^M, LF and CRLF, and the cross-platform recommendations. - Documentation/config/core.adoc: the source text for
core.autocrlf,core.eolandcore.safecrlf. - git-add:
--renormalize. - git-ls-files:
--eol,i/,w/andattr/.
Links and content were checked on 30 September 2026.