fatal: CRLF would be replaced by LF in somefile.txt
Preserving line endings in git by unsetting the text attribute in .gitattributes instead of converting files
I got an exciting new-to-me error from git today!
❯ git add .
fatal: CRLF would be replaced by LF in some-text-file-from-elsewhere.txt
A bit of googling found about ten thousand results all telling you to run the moral equivalent of dos2unix on it and move on. This is no good to me in this case - it's a text file I got from elsewhere
and I don't want it altered at any point, not by me now, and not by git
later if anyone happens to check it out on Windows. I spent a bit of time fiddling around with .gitattributes to try disable the
core.autocrlf and/or auto.safecrlf settings per file before I eventually found this excellent StackOverflow post
that mentions you just want to unset the text attribute on the file, which works out to be:
❯ echo "# this file came from the output of a command, so please don't mangle the line endings ever
some-text-file-from-elsewhere.txt -text" > .gitattributes
❯ git add .
❯
voila.