.gitattributes fix line endings on Windows + Cygwin/WSL

When using Git on Windows with Cygwin or Windows Subsystem for Linux, CRLF conflicts can falsely make your Git repo “dirty”. That is, git diff will show ^M at the end of each line and not let you merge code on git pull. This is very annoying by wasting time and possibly causing missed code changes and needless commits.

In my opinion the most robust approach is to force LF line-endings no matter what environment the user is in.

Fix Git Windows CRLF line-ending hell

Create in each Git repo a file .gitattributes containing:

*.py text eol=lf
*.c text eol=lf
*.cpp text eol=lf

Those lines are for Python and C/C++ respectively.

DO NOT just use * as that can goof up binary files in your repo.

Why not use .git/config for CRLF?

These are user-set so you’d have to have each user/computer manually configure these parameters. Quite annoying to set this manually for each repo. That’s why I instead include a .gitattributes in each repo as above to force line-endings to LF, so that no one has to think about CRLF even in the complex Windows + Cygwin or WSL line-ending case.

Leave a Comment