23 October 2018

LF would be replaced by CRLF


When collaborating across environments with Git you'll often get the insanely annoying:
"Fatal: LF would be replaced by CRLF in..."

To my mind this message is both backwards and lacking information:
If you check it out/or clone to another folder with your current core.autocrlf configuration, LF will be replaced by CRLF

The file will have its original line endings in your (current) working directory.

Ultimately it all comes down to the line endings used by both systems explained in detail on the pages:

Fixing The Problem

Convert individual files' line endings:

  • Windows: Notepad++
    EOL Conversion: choose the one you want.
  • Mac: dos2unix
    CRLF to LF: unix2dos [filename]
    LF to CRLF: dos2linx [filename] 

Set autocrlf setting

Default is false which won't change anything BUT this won't work cross platform: if you create a file on Windows it will be left as CRLF.
  • Check the setting: git config --global core.autocrlf
  • Ensure you have line endings in the repo:
    • git config --global core.autocrlf input
  • Add guard comparing if the reversed newline transformation would result in the same file:
    • git config --global core.safecrlf warn

Add a .gitignore file

Convenient list of files by alexkaratarakis here
Example for a c# project:

# These files are text and should be normalized (convert crlf => lf)
*.cs      text diff=csharp
*.xaml    text
*.csproj  text
*.sln     text
*.tt      text
*.ps1     text
*.cmd     text
*.msbuild text
*.md      text

# Images should be treated as binary
# (binary is a macro for -text -diff)
*.png     binary
*.jepg    binary

*.sdf     binary

Reset the Git repository

If you've got a repository with mixed line endings. you'll need to reset all the files to use LF:
  • Linux: 
    • git config --global core.safecrlf warn
    • dos2unix **
    • git add -u
  • GitHub for Windows has dedicated "normalize your repo's CRLF" setting
See also:

Check Your Editor Settings

  • VSCode: "files.eol": "\n"

No comments:

Post a Comment