14

I have files checked out with CRLF. I changed git config --global core.autocrlf false. Git doesn't see any modifications. When I edit a file, Git thinks the whole file changed. How do I re-checkout and overwrite all the files with LF instead of CRLF, so the diff works as expected? I saw this but the answer referred to an invalid command. I saw this but it said "Git wants to commit files that you have not modified" which is not true. I tried

git pull --force
git checkout --force

However the files still have CRLF. So how do I convert all the files from CRLF to LF? I can edit each & every file in Notepad++ and use Edit > EOL Conversions > Unix (LF), which is what I've been doing on a file by file basis, but it's very tedious. There is probably an awk or sed script also, but I'm not familiar with them, and I'd prefer to only modify what is checked into Git, and not mess up .git/.


Also tried

git reset --hard origin/master

And the files are still with CRLF!


This is not a duplicate of the suggested duplicate because I don't want to commit anything. Only want to fetch a fresh copy from Git. Also, Git doesn't see any files as being modified.

Chloe
  • 25,162
  • 40
  • 190
  • 357
  • Possible duplicate of [Git refuses to reset/discard files](https://stackoverflow.com/questions/18536863/git-refuses-to-reset-discard-files) – phd May 15 '18 at 15:59
  • Not sure why you think that is a duplicate. I mentioned "Git doesn't see any modifications." I don't have a problem committing. And it shouldn't be necessary to commit anything as nothing changed. Only seeking to fetch the original LF line endings. – Chloe May 15 '18 at 16:06
  • The answers there explain how to make `git reset --hard` work: ­first remove index either with `rm .git/index` or clear it with `git rm -r --cached .` – phd May 15 '18 at 16:08
  • You'd better fix your problem by using a '.giattributes' file to specify file end of lines config (which is the nowadays the good practice to solve your problem) and it will fix your problem for all your files. – Philippe May 16 '18 at 06:18
  • Well my files don't really have a problem. It was set to `true` and Git faithfully converted LF to CRLF. But I wanted to set it to `false` and just fetch a fresh copy of the files without any conversions, because my editors can handle LF. If someone checks in a CRLF file, I don't necessarily want to convert it to LF - I just want to see the original binary version of the file that's in Git. CRLF is fine too, if that's how it is in Git. – Chloe May 16 '18 at 16:11
  • If you were on Mac or Linux, you could run [`dos2unix`](https://en.wikipedia.org/wiki/Unix2dos) on each file in your directory. Maybe one of the alternative commands such as the `perl` one will work for you. – Rory O'Kane May 16 '18 at 16:17
  • I tried `dos2unix` but did not have it, even though I had DevKit. But it turned out I had `dos2unix` in Cygwin but apparently wasn't on my DOS path. – Chloe May 16 '18 at 16:20

2 Answers2

20

This seemed to work, but I'm not sure why. It looks very scary with lots of changes, but it ended up not changing anything. I got worried after the first line.

git rm --cached -r .
git reset --hard
git checkout .

No commits necessary.

RustyTheBoyRobot
  • 5,891
  • 4
  • 36
  • 55
Chloe
  • 25,162
  • 40
  • 190
  • 357
2

git add --renormalize . and then committing all changed files did it for me.

As per man git add it specifically mentions to run that after core.autocrlf change.

--renormalize

Apply the "clean" process freshly to all tracked files to forcibly add them again to the index. This is useful after changing core.autocrlf configuration or the text attribute in order to correct files added with wrong CRLF/LF line endings. This option implies -u.

elixon
  • 1,123
  • 12
  • 15
  • 1
    While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation. – SherylHohman May 19 '20 at 22:35