96

I'm using Ubuntu 13.10 x64, and I am working on a project that some developers are using Windows , I recently changed the git config core.eol to "lf" and core.autocrlf to "input" and core.safecrlf to "true". Since then, when I try to commit file into my local repository, I get this error:
fatal: CRLF would be replaced by LF in ......
From what I understand, if I set core.eol to "lf" and core.autocrlf to "input", git will automatically convert CRLF to LF, but why this error come out? How can I fix this problem?

Thank you.

aserww106
  • 1,311
  • 3
  • 12
  • 14

9 Answers9

236

This is a classic issue:

http://toub.es/sites/toub.es/files/styles/standard_article/public/field/image/firstcommit.png
(picture from Luis Tubes's blog post)

The usual fix is to convert those files yourself, with dos2unix or Swiss File Knife.

I have always preferred to keep core.autocrlf to false, which means:

git config --global core.autocrlf false
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • That error message even creeps in the `git diff` output: http://git.661346.n2.nabble.com/git-diff-returns-fatal-error-with-core-safecrlf-is-set-to-true-td7590136.html – VonC Nov 23 '13 at 22:42
  • 11
    Why git can't change CRLF to LF for me if I already set `core.autocrlf` to input ? – aserww106 Nov 23 '13 at 23:30
  • 1
    @William because you are working on Linux, and with files coming from Windows. – VonC Nov 23 '13 at 23:31
  • Thank you, @VonC, I already use dos2unix to change all files eol, So when the Windows developers commit some code to their repo,if I pull from their repository, git will convert CRLF to LF , right? Our git server is on Ubuntu. – aserww106 Nov 23 '13 at 23:45
  • @William yes (although I really don't like such "global" changes). Is it possible some files weren't changed, before you modify the config of this repo, and are now affected because you modified and want to git add them to the index? – VonC Nov 23 '13 at 23:49
  • I'm not sure what do you mean? sorry for my bad English. If I changed all files eol to LF, then commit and push, will it effect those Windows developer when they pull from my repo then try commit?Will they see same error `fatal: LF would be replaced by CRLF in ...` – aserww106 Nov 23 '13 at 23:59
  • 2
    @William I mean that you said "I recently changed the `git config core.eol` to "`lf`" and `core.autocrlf` to "`input`"": that doesn't change the files already there. That would have an impact on future `git pull`. The current files are still in CRLF, and if modified, are converted to LF if possible, and, if not, triggers the error message you mention. – VonC Nov 24 '13 at 00:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/41781/discussion-between-william-and-vonc) – aserww106 Nov 24 '13 at 00:11
  • Addendum: The dos2unix CLI is most easily installed via Homebrew (and not npm). – 2540625 Aug 07 '15 at 18:46
  • i converted the file using dos2unix, and it works great. Does anyone knows if core.autocrlf false has any bad effect for windows developers? And what about core.safecrlf false? Is it also necessary? – Ahmet Cetin Dec 23 '15 at 17:51
  • @AhmetCetin no, no bad effect for Windows user: `core.autocrlf false` is fine. safecrlf is safer: http://stackoverflow.com/a/21046682/6309, but I prefer using gitattributes: http://stackoverflow.com/a/10855862/6309 – VonC Dec 23 '15 at 20:16
  • Doe not work for me in Windows, still get this error – Aleksey Kontsevich Apr 13 '16 at 10:27
  • @AlekseyKontsevich Strange. Can you ask a new question, with the exact version of your git and Windows? – VonC Apr 13 '16 at 10:30
  • Solved for now with git config core.safecrlf warn – Aleksey Kontsevich Apr 13 '16 at 10:37
66

I had the same problem and tried the suggested solution with no success.

I had to execute a second command to make it work:

$ git config --global core.autocrlf false
$ git config --global core.safecrlf false
almo
  • 6,107
  • 6
  • 43
  • 86
30
$ git config core.autocrlf false
Tim
  • 41,901
  • 18
  • 127
  • 145
Arun
  • 2,800
  • 23
  • 13
  • 4
    I don't know what this does but it works. The fatal warning goes away and I'm no longer scared. – wh1tney Aug 09 '14 at 15:36
  • I did this and now `git diff` sees my entire file (1000 lines) as a conflict. The diff tools only see 3 line changes. – Dagrooms Jul 14 '15 at 13:24
15

One may just try dos2unix:

dos2unix [filename]
Yola
  • 18,496
  • 11
  • 65
  • 106
6

This happened to me on thousands of files. So I wrote a quick bash script to make dos2unix fix it for me. Someone else on Linux or Mac might find it useful.

#!/usr/bin/env bash

unwindows() {

  local errmsg
  local fpath

  # base case
  errmsg="$(git add . 2>&1)"
  if [[ $? -eq 0 ]]; then
    echo 'Successfully converted CRLF to LF in all files.'
    echo 'Successfully ran "git add .".'
    echo 'Done.'
    return 0
  fi

  fpath="${errmsg#*fatal: CRLF would be replaced by LF in }"
  fpath="${fpath%.*}"

  if [[ "${fpath}" == "${errmsg}" ]]; then
    err 'Regex failed. Could not auto-generate filename from stderr.'
    return 1
  fi

  if [[ ! -e "${fpath}" ]]; then
    err "Regex failed. '${fpath}' does not exist."
    return 1
  fi

  if ! dos2unix "${fpath}"; then
    err "Failed to run \"dos2unix '${fpath}'\"."
    return 1
  fi

  # recursive case
  unwindows
}

err() {
  local -r msg="$1"
  echo "${msg}" >&2
}

unwindows

Basically, it tries to do git add .. If the command fails, it grabs the name of the incompatible file from the error output. Then it runs dos2unix on that file. It keeps repeating this process until git add . works.

If you run this, you should see dos2unix: converting file xxx to Unix format... repeatedly. If you don't, it's not working, so just press ctrl+c or command+c to stop it.

GreenRaccoon23
  • 3,603
  • 7
  • 32
  • 46
  • 2
    In case anyone's curious how I managed to rack up thousands of uncommitted files, it's because the repo has a bunch of code-generated images. I wasn't postponing a commit for 3 years or anything. – GreenRaccoon23 Nov 05 '16 at 05:16
2

FYI not sure if this applies to you but I was getting this error when accidentally trying to add all node_modules to the staged changes. So actually .gitignoring the node_modules solved my problem.

Nickofthyme
  • 3,032
  • 23
  • 40
1

You need to add all files that git status displays as modified:

git add file1
git add file2

And then commit your changes :

git commit

This will keep your local files as is, but will autocrlf them on the remote repository.

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Karl.S
  • 2,294
  • 1
  • 26
  • 33
1

I faced same trouble and fixed with editing .gitattributes as below.

$ vim .gitattributes

comment out 2 lines in .gitattributes

-* text=auto
-* text eol=lf
+# * text=auto
+# * text eol=lf
a10a
  • 211
  • 2
  • 10
1

I'm on a Mac using Terminal and had this problem with an .htaccess file I was trying to commit, getting the fatal error:

fatal: CRLF would be replaced by LF in .htaccess

I wanted to fix the problem, like the OP requests, not just turn off a git flag, so I found this article that gives a perl command to fix the problem on a per file basis.

perl -pi -e 's/\r\n/\n/g' input.file

So for my .htaccess error above, I ran the following:

perl -pi -e 's/\r\n/\n/g' .htaccess 

The flags -p, -i and -e (pie) can be combined to allow you to edit files using Perl from the command line. In this case replacing all \r\n found with \n.

thetwopct
  • 1,551
  • 1
  • 11
  • 13