v.pandelane.dev

Line endings in Git on Windows without surprises

2026-08-02

The symptom: a shell script that works on the server fails after it has been through a Windows checkout, with /bin/sh^M: bad interpreter. The cause is core.autocrlf=true, which rewrites every text file to CRLF on checkout.

The fix that travels with the repository

core.autocrlf is a per-machine setting, so fixing it on one machine fixes nothing for the next person. A .gitattributes file is committed and applies to everyone:

*.sh       text eol=lf
*.service  text eol=lf
*.bat      text eol=crlf

Files that match keep the stated line ending in the working tree no matter what the local setting is.

Files that are already wrong

Adding the attribute does not touch files already checked out. Re-normalize once and commit the result:

git add --renormalize .
git status

Checking a file

git ls-files --eol shows the line endings in the index and in the working tree side by side, which settles the question of where the CRLF came from.