Line Endings

Line Endings, Autocrlf and Text Attributes

Windows ends lines with CR LF, Linux and macOS with LF. Mixed endings make every line look changed, and a CRLF shell script fails in a Linux container. git ls-files --eol shows the ending in the index (i/), in the working tree (w/) and the attribute in force (attr/):

Git stores whatever line endings it is givenShell
mkdir eol && cd eol && git init -q
printf 'first line\r\nsecond line\r\n' > windows.txt
printf 'first line\nsecond line\n' > unix.txt
git add .
git ls-files --eol
Output
i/lf    w/lf    attr/                   unix.txt
i/crlf  w/crlf  attr/                   windows.txt

Git for Windows 47,417 ' installer sets core.autocrlf=true (commit LF, check out CRLF), but that lives on one machine. A committed .gitattributes file binds every clone:

Normalizing line endings with .gitattributesShell
printf '* text=auto eol=lf\n*.bat text eol=crlf\n*.png binary\n' > .gitattributes
git add --renormalize .
git add .gitattributes
git ls-files --eol
Output
i/lf    w/lf    attr/text=auto eol=lf   .gitattributes
i/lf    w/lf    attr/text=auto eol=lf   unix.txt
i/lf    w/crlf  attr/text=auto eol=lf   windows.txt

text=auto eol=lf keeps text files LF everywhere, *.bat keeps CRLF for cmd.exe, and binary disables conversion and text diffs. --renormalize rewrote the staged windows.txt to LF; the working copy follows at its next checkout. Commit .gitattributes first: renormalizing a mature project touches every file in one commit.