Windows git "warning: LF will be replaced by CRLF", is that warning tail backward?

Git

Git Problem Overview


env:

  • Windows 7
  • msysgit

Wheng I git commit, it says:

warning: LF will be replaced by CRLF. 

Is this warning tail backward?
I edit file in Windows, the end of line is CRLF, just like this pic:
enter image description here
And git changes it to LF for committing to repo.
So I think the correct warning is:

warning: CRLF will be replaced by LF. 

Git Solutions


Solution 1 - Git

> warning: LF will be replaced by CRLF.

Depending on the editor you are using, a text file with LF wouldn't necessary be saved with CRLF: recent editors can preserve eol style. But that git config setting insists on changing those...

Simply make sure that (as I recommend here):

git config --global core.autocrlf false

That way, you avoid any automatic transformation, and can still specify them through a .gitattributes file and core.eol directives.


> windows git "LF will be replaced by CRLF"
Is this warning tail backward?

No: you are on Windows, and the git config help page does mention

> Use this setting if you want to have CRLF line endings in your working directory even though the repository does not have normalized line endings.

As described in "git replacing LF with CRLF", it should only occur on checkout (not commit), with core.autocrlf=true.

       repo
    /        \ 
crlf->lf    lf->crlf 
 /              \    

As mentioned in XiaoPeng's answer, that warning is the same as:

> warning: (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.

As mentioned in git-for-windows/git issue 1242:

> I still feel this message is confusing, the message could be extended to include a better explanation of the issue, for example: "LF will be replaced by CRLF in file.json after removing the file and checking it out again".

Note: Git 2.19 (Sept 2018), when using core.autocrlf, the bogus "LF will be replaced by CRLF" warning is now suppressed.


As quaylar rightly comments, if there is a conversion on commit, it is to LF only.

That specific warning "LF will be replaced by CRLF" comes from convert.c#check_safe_crlf():

if (checksafe == SAFE_CRLF_WARN)
  warning("LF will be replaced by CRLF in %s.
           The file will have its original line endings 
           in your working directory.", path);
else /* i.e. SAFE_CRLF_FAIL */
  die("LF would be replaced by CRLF in %s", path);

It is called by convert.c#crlf_to_git(), itself called by convert.c#convert_to_git(), itself called by convert.c#renormalize_buffer().

And that last renormalize_buffer() is only called by merge-recursive.c#blob_unchanged().

So I suspect this conversion happens on a git commit only if said commit is part of a merge process.


Note: with Git 2.17 (Q2 2018), a code cleanup adds some explanation.

See commit 8462ff4 (13 Jan 2018) by Torsten Bögershausen (tboegi).
(Merged by Junio C Hamano -- gitster -- in commit 9bc89b1, 13 Feb 2018)

> ## convert_to_git(): safe_crlf/checksafe becomes int conv_flags

> When calling convert_to_git(), the checksafe parameter defined what should happen if the EOL conversion (CRLF --> LF --> CRLF) does not roundtrip cleanly.
In addition, it also defined if line endings should be renormalized (CRLF --> LF) or kept as they are.

> checksafe was an safe_crlf enum with these values:

SAFE_CRLF_FALSE:       do nothing in case of EOL roundtrip errors
SAFE_CRLF_FAIL:        die in case of EOL roundtrip errors
SAFE_CRLF_WARN:        print a warning in case of EOL roundtrip errors
SAFE_CRLF_RENORMALIZE: change CRLF to LF
SAFE_CRLF_KEEP_CRLF:   keep all line endings as they are

Note that a regression introduced in 8462ff4 ("convert_to_git(): safe_crlf/checksafe becomes int conv_flags", 2018-01-13, Git 2.17.0) back in Git 2.17 cycle caused autocrlf rewrites to produce a warning message despite setting safecrlf=false.

See commit 6cb0912 (04 Jun 2018) by Anthony Sottile (asottile).
(Merged by Junio C Hamano -- gitster -- in commit 8063ff9, 28 Jun 2018)

Solution 2 - Git

YES the warning is backwards.

And in fact it shouldn't even be a warning in the first place. Because all this warning is saying (but backwards unfortunately) is that the CRLF characters in your file with Windows line endings will be replaced with LF's on commit. Which means it's normalized to the same line endings used by *nix and MacOS.

Nothing strange is going on, this is exactly the behavior you would normally want.

This warning in it's current form is one of two things:

  1. An unfortunate bug combined with an over-cautious warning message, or
  2. A very clever plot to make you really think this through...

;)

Solution 3 - Git

--Update on 9th July---

Removed "It is correct and accurate" as commented by @mgiuca

======

NO. It is NOT talking about your files currently with CRLF. It is instead talking about files with LF.

It should read:

> warning: (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.

This picture should explain what it means. enter image description here

Solution 4 - Git

All of this assumes core.autocrlf=true

Original error:

> warning: LF will be replaced by CRLF
> The file will have its original line endings in your working directory.

What the error SHOULD read:

> warning: LF will be replaced by CRLF in your working directory
> The file will have its original LF line endings in the git repository

Explanation here:

> The side-effect of this convenient conversion, and this is what the warning you're seeing is about, is that if a text file you authored originally had LF endings instead of CRLF, it will be stored with LF as usual, but when checked out later it will have CRLF endings. For normal text files this is usually just fine. The warning is a "for your information" in this case, but in case git incorrectly assesses a binary file to be a text file, it is an important warning because git would then be corrupting your binary file.

Basically, a local file that was previously LF will now have CRLF locally

Solution 5 - Git

git config --global core.autocrlf false works well for global settings.

But if you are using Visual Studio, might also need to modify .gitattributes for some type of projects (e.g c# class library application):

  • remove line * text=auto

Solution 6 - Git

This happens because the configuration for GitHub Desktop on Windows assumes CRLF but the text editor may be using LF. You can change your local repository settings to use lf instead.

Navigate to the root of the git repo and execute this in exact same order

git config core.eol lf
git config core.autocrlf input

Source: GitHub issue

Solution 7 - Git

After I set core.autocrlf=true I was getting "LF will be replaced by CRLF" (note not "CRLF will be replaced by LF") when I was git adding (or perhaps it was it on git commit?) edited files in windows on a repository (that does use LF) that was checked out before I set core.autocrlf=true.

I made a new checkout with core.autocrlf=true and now I'm not getting those messages.

Solution 8 - Git

I faced similar issue and using vscode(v1.57) on windows tried solutions defined in other answers but didn't worked.

So for me following steps worked:

  1. In root folder there is a file named .editorconfig
  2. open this file and change end_of_line = lf with end_of_line = crlf
  3. run git rm --cached and warning gone!!

Solution 9 - Git

Make sure you added unnecessary files or folders in .gitignore file.

e.g. node_modules

if still face then run this command

``git config --global core.autocrlf false```

Solution 10 - Git

Close Visual Studio

If you get the error, a simple fix is closing Visual Studio and you can commit to main, it is that easy. I had this same problem and that was how I fixed it. It is caused because the files you want to open are open in another programm.

Solution 11 - Git

Do just simple thing:

  1. Open git-hub (Shell) and navigate to the directory file belongs to (cd /a/b/c/...)
  2. Execute dos2unix (sometime dos2unix.exe)
  3. Try commit now. If you get same error again. Perform all above steps except instead of dos2unix, do unix2dox (unix2dos.exe sometime)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHonghe.WuView Question on Stackoverflow
Solution 1 - GitVonCView Answer on Stackoverflow
Solution 2 - GitStijn de WittView Answer on Stackoverflow
Solution 3 - GitDevs love ZenUMLView Answer on Stackoverflow
Solution 4 - GitmikewView Answer on Stackoverflow
Solution 5 - GitEricView Answer on Stackoverflow
Solution 6 - GitavpView Answer on Stackoverflow
Solution 7 - GitMarsette VonaView Answer on Stackoverflow
Solution 8 - GitAjayView Answer on Stackoverflow
Solution 9 - GitGMKHussainView Answer on Stackoverflow
Solution 10 - GitIndigo SoftwaresView Answer on Stackoverflow
Solution 11 - GitAntriksh JainView Answer on Stackoverflow