Make Git "LF will be replaced by CRLF" warnings go away

Git

Git Problem Overview


I have setup Git so it doesn't commit inconsistent line endings. The problem with that is a whole pile of files appear modified even though they are not. What do I type to make these files have the line endings fixed on the local side?

# git checkout dev
M	src/au/policy/dao/EmailQueue.java
M	src/au/policy/dao/EmailQueueFactory.java
M	src/au/policy/dao/PolicyPublisher.java
Already on 'dev'

# git diff
warning: LF will be replaced by CRLF in src/au/policy/dao/EmailQueue.java
warning: LF will be replaced by CRLF in src/au/policy/dao/EmailQueueFactory.java
warning: LF will be replaced by CRLF in src/au/policy/dao/PolicyPublisher.java

This is what I added to my git config file which seems to do what I intended aside from this issue:

autocrlf = true

Git Solutions


Solution 1 - Git

This might happen if you change core.autocrlf config variable (if I understand your problem correctly).

If you are at clean state, i.e. just after commit, and you don't have uncomitted changes, forced re-checkout and removing index should do the trick:

>The below command git reset --hard HEAD will make your current branch to point to the latest commit and all uncommitted code will be lost. Make sure to commit the code or take the backup

$ rm .git/index
$ git reset --hard HEAD

That, I think, would sync both working area files, and the index (staging area) to follow crlf settings.

Solution 2 - Git

I had this problem when creating new Xcode project. My solution for this problem:

In terminal write

$: git config --global --edit

Then in git config file change safecrlf to false. My settings:

[core]
    autocrlf = input
    safecrlf = false

I know git have cmd line tools for this but they don't work for me. And then Xcode create git repos without any problem.

Solution 3 - Git

Only thing I can think of is to check if core.safecrlf is set to warn.

git config --get core.safecrlf

I think possible values are true, false, and warn. I believe that setting to false will resolve the warning, though it may not be a good idea.

Solution 4 - Git

You can just delete and re-checkout the offending files from the index like this:

rm <files>
git checkout -- <files>

Or, if they are the only modified files (be careful with this command), you can script it like this:

git diff --name-only --diff-filter=M | xargs rm --
git checkout -- .

On a GNU system you can use a slightly safer pipe, but you don't appear to have spaces or other delimiting characters in your filenames in any case.

git diff -z --name-only --diff-filter=M | xargs -0 rm --

Solution 5 - Git

Try this, it worked for me:

cd src/au/policy/dao
dos2unix

If there are other files in that folder, then you'll want to break it up into the following (otherwise it will try to do it on every file in any subdirectories, which may take a while):

cd src/au/policy/dao
dos2unix EmailQueue.java
dos2unix EmailQueueFactory.java
dos2unix PolicyPublisher.java

It ran really quick on my machine and fixed all of the line endings, and it's a little simpler and easier than some of these other fixes.

Solution 6 - Git

Note that note all of the above fixes may work for you, for example you might have received the code through a simple file transfer. You can accept each warning by pressing ENTER, but on on large repos that can take ages. Another way to ignore the conversion on code that has already been checked-out and edited is to create a patch file:

git diff > changes.patch

Solution 7 - Git

In my case, after setting autocrlf variable to true, then running

git stash

and then

git stash pop

everything cleared out

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
QuestioncorydorasView Question on Stackoverflow
Solution 1 - GitJakub NarębskiView Answer on Stackoverflow
Solution 2 - GitSoniqueView Answer on Stackoverflow
Solution 3 - GitBob AmanView Answer on Stackoverflow
Solution 4 - GitCB BaileyView Answer on Stackoverflow
Solution 5 - GitAlexander MillerView Answer on Stackoverflow
Solution 6 - GitGeoffrey VLView Answer on Stackoverflow
Solution 7 - GitEugenio MiróView Answer on Stackoverflow