Reputation: 1341
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.
Upvotes: 98
Views: 107653
Reputation: 6367
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
Here is the Git documentation for core.autocrlf
:
core.autocrlf
Setting this variable to "true" is the same as setting the
text
attribute to "auto" on all files and core.eol to "crlf". Set to true if you want to have CRLF line endings in your working directory and the repository has LF line endings. This variable can be set toinput
, in which case no output conversion is performed.
Here is the Git documentation for core.safecrlf
:
core.safecrlf
If true, makes Git check if converting CRLF is reversible when end-of-line conversion is active. Git will verify if a command modifies a file in the work tree either directly or indirectly. For example, committing a file followed by checking out the same file should yield the original file in the work tree. If this is not the case for the current setting of
core.autocrlf
, Git will reject the file. The variable can be set to "warn", in which case Git will only warn about an irreversible conversion but continue the operation.[... rest of documentation cut for brevity ...]
Upvotes: 69
Reputation: 19043
One may just try dos2unix
(man page):
dos2unix [filename]
This converts all the CRLF line breaks in a file to LF line breaks.
Upvotes: 16
Reputation: 1681
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.
Upvotes: 1
Reputation: 4327
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.
Upvotes: 2
Reputation: 3833
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.
Upvotes: 6
Reputation: 211
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
Upvotes: 1
Reputation: 2402
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.
Upvotes: 0
Reputation: 1324937
This is a classic issue:
(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
Upvotes: 238