Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Concepts

Line endings (CRLF and LF)

Handle CRLF and LF line endings in Git: how core.autocrlf, core.eol and .gitattributes text settings work, and how to normalize a repository.

Windows ends text lines with two characters (CRLF, \r\n); Linux and macOS use one (LF, \n). In a team that mixes systems, files can flip between the two, producing diffs where every line appears changed, or scripts that fail with bad interpreter: /bin/sh^M.

Git solves this by normalizing: it can store text files with LF in the repository and convert to the platform's ending when writing the working tree. The modern, reliable way to control it is a .gitattributes file committed with the project, which gives every contributor the same behavior. The older per-user setting is core.autocrlf.

TEXT
# .gitattributes
* text=auto
*.sh text eol=lf
*.bat text eol=crlf
*.png binary

Try it

Bash Normalizing with text=auto

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Forcing the working-tree ending with eol

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Settings

Setting Effect
core.autocrlf=true convert CRLF to LF on commit, LF to CRLF on checkout (typical for Windows)
core.autocrlf=input convert CRLF to LF on commit, never convert on checkout (typical for Linux/macOS)
core.autocrlf=false no conversion
core.eol=lf or crlf or native working-tree ending for files marked text
core.safecrlf=true or warn warn or fail when a conversion would not round-trip
text / text=auto / -text / binary attribute: always treat as text, let Git detect, never convert
eol=lf / eol=crlf attribute: working-tree ending for this path, whatever the platform

Attributes win over core.autocrlf. text=auto lets Git decide per file whether the content looks like text; use binary (or -text) for files that must never be touched.

Usage notes

  • Commit .gitattributes first, then normalize existing files with git add --renormalize . and commit the result in a dedicated commit. Blame can skip it with git blame --ignore-rev.
  • Check what Git thinks. git ls-files --eol shows i/ (index) and w/ (working tree) endings and the attribute applied to each path.
  • Warnings such as "LF will be replaced by CRLF the next time Git touches it" are informational. They show up when a conversion will happen, not when something is broken.
  • Shell scripts and Makefiles must keep LF on every platform; mark them eol=lf. Windows batch files expect CRLF.
  • Mixed endings inside one file are not normalized when text=auto considers the file ambiguous; fix such files by hand first.
  • Editors matter. An .editorconfig with end_of_line stops editors from reintroducing the wrong ending.
  • Binary files (images, archives) must never be converted. If a file was corrupted by conversion, mark it binary and recommit it.
  • Whitespace checks treat a trailing CR as an error unless core.whitespace=cr-at-eol is set (see core.whitespace).