loading...

ELI5: The git error - 'CRLF would be replaced by LF in...'

hiboabd profile image Hibo Abdilaahi ・1 min read

It's happened a couple of times and the explanations on google I found weren't helping...

I would like to understand it once and for all!

Discussion

markdown guide
 

Honestly, that message has always been confusing.

If you have git setup on Windows using Windows line endings (CRLF), I think you get that message when committing, right? You're getting that message because git has to convert between Windows line endings and Unix line endings (LF). It converts LF->CRLF on checkout, and CRLF->LF on commit. This only happens if your core.autocrlf setting is set to true (if I remember correctly).

CR and LF are 2 different whitespace characters. Older Macs used to use CR for line endings. Windows uses both combined, and Linux (and I believe newer Macs) uses LF.

Windows 10 now does support LF I think, and the code editors I've been using for years now supported LF before that (Atom, and now VS Code), so I try to stick with only LF these days. I've disabled core.autocrlf on my machine.

However, if you work with people who do use native Windows line endings, that is something everyone should be aware of when setting up git. This could even be configured per repository depending on weather most of your projects are collaborative or not. I'm usually the only one writing code where I work, so I get to make the rules. Lol

 

Thanks for taking the time out to write this detailed response! It’s nice to know others find it confusing too haha.

Yes you do get that message when committing and it prevents you from doing so. This explanation has helped me understand the error somewhat but I don’t see why I would get the message sometimes and not other times 😭 I think I need to keep a better eye on what I’m committing when the error happens which might shed some more light...

Thanks again!!

 

It may depend on whether there's a mix of line ending styles in a file. I haven't really looked into it myself. I just got sick of the warnings at some point and switched completely over to LF.

If I remember correctly, there may also be a way to completely disable warnings from Git. But I think it disables all of them, not just this one.

Happy to help when I can. Sorry I wasn't able to help resolve that specific issue.

Ahhh thanks I’ll look into switching for the future! No problem at all, you’ve already been helpful and I was able to easily get past the error. It’s just not the first time it’s happened so I’m wondering how to prevent it full stop, if possible.

 

To make a new line there is an invisible character or characters at the end of the line.
In windows it's two characters a "carriage return" (\r) and a "line feed" (\n) in most every other operating system it's just a "line feed". This why sometimes if you open a document in notepad on Windows that you created in, for instance, MacOS, all the text will be on a single line and it will look messed up.
Anyway, git is smart and it knows about how windows and MacOS etc show new lines. To make sure the code looks correct on any operating system it can turn your CRLF on windows into a LF on MacOS or the going the other way, a LF will turn into a CRLF. But needs to be told that is what it should do. If you would like more information about how to change and set that up look here: docs.github.com/en/github/using-gi...

More in depth reading can be found here: en.wikipedia.org/wiki/Newline#Issu... about newlines.

 

Wow this was super clear! Thanks so much I understand it so much better now.

Only puzzling thing is that I was only using a Mac so not sure why this error happened as I would only have line feed spacing right? Will probably do a bit more googling around it but thanks again! Super helpful 🀩

 

Probably something to do with getting source code from a windows computer at some point I would guess? In essence likely some developer worked on windows and then it messed up some line endings for a particular file or files. So now git is telling you about it even on MacOS

 
 

Another tip for setting LF on windows, If you're using VSC, be sure set this Eol (end of line) setting to "\n"

VSC Eol setting