11

I'm using Visual Studio code and have inherited a project that uses 'LF' line endings. By default Visual Studio Code opens my files in CRLF (i'm using Windows 10) which causes my git pre-commit hooks to fail. I don't get any error messages if I switch Visual Studio Code to use LF at the bottom right of the screen but obviously i'd like to avoid having to switch this every time I edit and try to commit a file.

I followed the instructions from this question and my understanding was that changing the files.eol setting to "\n" would open files in LF but it still opens them in CRLF.

I'm a little new to this stuff so please bear with me but if someone could tell me what i'm doing wrong that would be great.

Worth mentioning that the project has an editorconfig file with end_of_line setting as LF and changing this to CRLF also doesn't help

Rajendran Nadar
  • 4,962
  • 3
  • 30
  • 51
red house 87
  • 1,837
  • 9
  • 50
  • 99

4 Answers4

32

Note - Make sure you don't have any uncommitted changes else it will be deleted when you run the following cmd!

Run this in your terminal or cmd prompt

git config core.autocrlf false
git rm --cached -r .
git reset --hard

This usually happens when the project is created on UNIX based system & then used on a windows system because both have different line endings.

We need to disable auto CRLF on git & uncommit & recommit changes.

Reference

Rajendran Nadar
  • 4,962
  • 3
  • 30
  • 51
  • 2
    This worked. We had a dev on Windows with WebStorm opening every file and finding linting errors about CRLF. The file in the repo was authored and committed by a dev on OS X, so we could not figure out why the dev on Windows was getting CRLF errors before they had even altered the file. Following this solution on the Windows machine fixed it. – jakxnz Jun 27 '20 at 22:54
3

You can set the default end of line character in VSCode under File > Preferences > Settings > Files:Eol

erica mitchell
  • 577
  • 4
  • 5
0

When cloning a project, Git automatically converts LF to CRLF which causes the error.

git config --global core.autocrlf false

To avoid this automatic conversion, run “git config --global core.autocrlf false” in Git Bash before cloning the project.

crazwade
  • 51
  • 1
  • 5
0

1.VSCode settings:
Set the end of line character in

File > Preferences > Settings > Files:Eol

2.Set core.autocrlf to false in gitconfig:

Before you modify the line endings, run the following command to disable automatic line ending conversion:

git config core.autocrlf false

2.Run Line Ending Conversion: After disabling automatic line ending conversion, Run the following command to change the line endings of all tracked files in your repository from CRLF to LF:

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

For above step, Backup Your Changes and ensure that you don't have any uncommitted changes that you want to keep before proceeding.

mnnim
  • 31
  • 2