-
Notifications
You must be signed in to change notification settings - Fork 0
End of Line (CR LF, LF)
- On Windows:
git config --global core.autocrlf true
When committing a text file to the repository git replaces CR+LF with LF.
When checking out a text file from the repository git replaces LF with CR+LF.
- On Linux (or MacOS):
git config --global core.autocrlf input
When committing a text file to the repository git replaces CR+LF with LF.
When checking out a text file from the repository git does not do any replacement.
When trying to add a file that contains CR+LF to the index (staging area),
git refuses to do it and displays this fatal error
fatal: CRLF would be replaced by LF in file_with_crlf.txt
This is because of a Windows file containing CR+LF line-endings was added to working directory.
git add file_with_crlf.txt
Error: fatal: CRLF would be replaced by LF in file_with_crlf.txtFYI, I'm running git on MacOS and configured git like so git config --global core.crlf input,
which means that when I commit text files to the repository git converts CR+LF into LF.
It however does not do this conversion the other way around (ie. when I check out files from the repository).
I add to the index a file with CR+LF (that comes from Windows) but git refuses to do so because this operation cannot be reversed. Why? When git stores this text file to the repository it replaces the CR+LF with LF, and this is perfectly fine. However, when copying this file from the repository to the working directory (checking it out) it leaves LF unchanged and won't perform the opposite operation.
Why this error?
Git uses this fatal error message to inform us the file may be inconsistent once checked out from the repository because it won't contain the CR+LF it had initially but LF instead.
- solution 1) convert
CR+LFtoLFbefore adding this file to the index (aka. staging area) usingdos2unix - solution 2) Set
core.safecrlftowarnin order to transform the above error into a warning and allow the conversion to occur - Solution 3) Use a
.gitattributesfile to define the line-ending conversions. Read more...
Here is how to convert CR+LF into LF in a text file file_with_crlf.txt (before adding it to the index).
dos2unix -k file_with_crlf.txt
git add file_with_crlf.txt # No fatal error this timewhere -k asks dos2unix to keep the file dates unchanged
When the value of core.safecrlf is either true or warn, Git checks if the conversion specified by core.autocrlf is not reversible:
- if
true, Git refuses to do the conversion (error) and prints an error messageError: fatal: CRLF would be replaced by LF. - if
warn, Git does the conversion but only prints the above warning message.