.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.

One 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
*.h text eol=lf
*.f text eol=lf
*.for text eol=lf
*.f90 text eol=lf

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

Another option

git config --global core.autocrlf false

This tells Git not to generate \r\n, and on Linux/Mac to force \n.

Tags: ,

Categories:

Written by Michael Hirsch, Ph.D. //

Comments