1810
git init
git add .

Gives the following warnings for many files:

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

What's the difference between LF and CRLF? What should I do about the warnings?

9
  • 60
    Nowadays just about any text editor or developer related tool you use will account for Unix/Windows line end differences. Except Notepad, but Notepad isn't so hot anyway :)
    – Matt Greer
    Apr 29, 2011 at 15:37
  • 1
    @Matt Greer Which means basically since i'm using Aptana Studios 3 IDE for Ruby on Rails it will cause this to happen? Apr 29, 2011 at 18:45
  • 5
    Warning is not bad. Worse is a msg like this fatal: LF would be replaced by CRLF in Gemfile.lock, and git doesn't allow you to add a file. It is produced if you have safecrlf = true option set in any (global or local) .gitconfig file. Just hide it under comment if any.
    – Green
    Mar 21, 2013 at 0:52
  • 15
    @MattGreer it is still a huge problem - if you get the line endings wrong on a shebang line, for example, a linux kernel can think the entire script is on one line, and the entire script is therefore the name of some executable to run. Or nearly just as bad, that the executable is called "/usr/bin/perl\cM" or whatever. Dec 31, 2013 at 16:56
  • 9
    If you are in a Unix system $ dos2unix file will fix this for you
    – ackuser
    Oct 3, 2015 at 19:00

2 Answers 2

2247

In Unix systems the end of a line is represented with a line feed (LF). In windows a line is represented with a carriage return (CR) and a line feed (LF) thus (CRLF). when you get code from git that was uploaded from a unix system they will only have an LF.

If you are a single developer working on a windows machine, and you don't care that git automatically replaces LFs to CRLFs, you can turn this warning off by typing the following in the git command line

git config core.autocrlf true

If you want to make an intelligent decision how git should handle this, read the documentation

Here is a snippet

Formatting and Whitespace

Formatting and whitespace issues are some of the more frustrating and subtle problems that many developers encounter when collaborating, especially cross-platform. It’s very easy for patches or other collaborated work to introduce subtle whitespace changes because editors silently introduce them, and if your files ever touch a Windows system, their line endings might be replaced. Git has a few configuration options to help with these issues.

core.autocrlf

If you’re programming on Windows and working with people who are not (or vice-versa), you’ll probably run into line-ending issues at some point. This is because Windows uses both a carriage-return character and a linefeed character for newlines in its files, whereas Mac and Linux systems use only the linefeed character. This is a subtle but incredibly annoying fact of cross-platform work; many editors on Windows silently replace existing LF-style line endings with CRLF, or insert both line-ending characters when the user hits the enter key.

Git can handle this by auto-converting CRLF line endings into LF when you add a file to the index, and vice versa when it checks out code onto your filesystem. You can turn on this functionality with the core.autocrlf setting. If you’re on a Windows machine, set it to true – this converts LF endings into CRLF when you check out code:

$ git config --global core.autocrlf true

If you’re on a Linux or Mac system that uses LF line endings, then you don’t want Git to automatically convert them when you check out files; however, if a file with CRLF endings accidentally gets introduced, then you may want Git to fix it. You can tell Git to convert CRLF to LF on commit but not the other way around by setting core.autocrlf to input:

$ git config --global core.autocrlf input

This setup should leave you with CRLF endings in Windows checkouts, but LF endings on Mac and Linux systems and in the repository.

If you’re a Windows programmer doing a Windows-only project, then you can turn off this functionality, recording the carriage returns in the repository by setting the config value to false:

$ git config --global core.autocrlf false
24
  • 201
    Try this git config --global core.safecrlf false to disable warning and keep it functioning. I got this command from here. Feb 6, 2016 at 12:27
  • 24
    core.autocrlf true does not turn off the warning for me, but core.safecrlf false as mentioned by Joel do.
    – Zitrax
    Mar 1, 2016 at 15:40
  • 7
    As @JoelHandwell mentioned, only git config --global core.safecrlf false suppressed my annoying warnings Oct 21, 2017 at 20:31
  • 29
    I think it should be stated that by setting core.safecrlf to false you are not "suppressing warnings." You are changing the behavior of how git checks in and checks out files, and it is that behavior that may or may not generate a warning. For me, I want it to be true because I am developing on Windows. However, I also wish to suppress the warnings, which is a hope that has no answer in this thread.
    – Bobort
    Jan 18, 2018 at 15:31
  • 7
    The warning asked about in this post states that "LF will be replaced by CRLF", but the scenarios described in this answer all involve replacing CRLF with LF (or doing nothing). I understand the behavior of the setting, but it doesn't explain the warning at all.
    – Mark
    May 6, 2020 at 14:13
439

If you want, you can deactivate this feature in your git core config using

git config core.autocrlf false

But it would be better to just get rid of the warnings using

git config core.autocrlf true
21
  • 126
    hmm weird, I just set core.autocrlf to true (and confirmed the git cli command returns the status as true). however I still get the warnings.
    – cavalcade
    Nov 13, 2012 at 11:03
  • 23
    Doesn't work for me. I had core.autocrlf false to begin with. Aug 16, 2013 at 13:21
  • 35
    My git config core.autocrlf is already set to true from the beginning, but the warnings still show up.
    – DLight
    Sep 14, 2014 at 22:59
  • 26
    I found that completely removing core.autocrlf from the .git/config file solved the problem for me. Dec 15, 2014 at 20:26
  • 21
    git config --global core.safecrlf false to disable the warning, not the answer given
    – omikes
    Jan 31, 2018 at 22:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.