1

In VSCode, I have this pop-up that says:

warning: LF will be replaced by CRLF in <xxx>

I thought it's a warning but my commit didn't go through.

I check and try to find a place to configure GIT inside VSCode. I read some advice like:

git config --get core.autocrlf 

I try to find some options in VSCode settings, but I just could not find a way to configure this in VSCode.

What's the correct way to configure this ? Thanks very much.

Data T
  • 117
  • 1
  • 8

4 Answers4

2

Outside VSCode, git config --global core.autocrlf false is a good option, as I mentioned here.

Inside VSCode, make sure the EOL (end of line) indicator (bottom right corner in the status bar) is the right one.
If your file has mixed line ending, there is no option to opt out of line ending normalisation for files, meaning, upon saving a file edited with VS Code, it appears to perform line ending normalisation automatically and without user notification of any sort.

Note: the warning message changes with Git 2.37 (Q3 2022) and becomes:

In the working copy of 'hello.txt', LF will be replaced by CRLF 
the next time Git touches it.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I'm using Git inside VSCode to commit, not on the command line. I don't know how to change the option git uses. How do I change it inside VSCode ? – Data T Jun 14 '22 at 01:45
  • 1
    @DataT You can use the [integrated terminal](https://code.visualstudio.com/docs/editor/integrated-terminal) and type those commands right from VSCode. – VonC Jun 14 '22 at 06:56
  • Thanks for the reply. I was thinking of using the versioning feature in VSCode to run GIT and therefore, I could configure the options inside. Is that possible for this issue ? Thanks. – Data T Jun 14 '22 at 07:14
  • 1
    @DataT Yes, you can configure Git from the terminal. And add a `.gitattributes` file to your source code in order to specify the EOL you want for specific files, as I have [done before](https://github.com/VonC/gitcred/blob/01d44899b82f8fcd5754a237328e2afbc6a03626/.gitattributes). For any other Git operation, I use the [GitLens VSCode extension](https://www.gitkraken.com/gitlens). – VonC Jun 14 '22 at 07:26
0

If you're working on Windows, you need to decide what line endings you want in your sandbox, and what line endings you want in your repos. The "right" choice for the autocrlf setting is not the same for everyone.

autocrlf=false: don't mess with my files!

@VonC's answer is the right answer if you just don't want Git to mess with line endings - an autocrlf set to false means take files in and out of Git verbatim.

This is arguably the only correct setting for autocrlf, because as @VonC pointed out, files like Windows .bat files will get broken by newline conversions, and you really do not want line-end conversions to happen in binary files either (that .jpg file might not be recoverable once you've accidentally done a CRLF->LF conversion on it...)

autocrlf=input: fix my mistakes when I create files

My usual answer on Windows is different, however: in my repos, I want Unix-style newlines, always. But on my Windows machine a number of pesky tools create files with Windows-style newlines. Yes, you can (and should) set your editor to use the right line endings, but an autocrlf set to input catches my "mistakes", e.g., when a file is generated by a Python script on a Windows machine, it usually ends up with CRLF line endings. In my Windows configurations, I always use git config --global core.autocrlf input, and that means my files always get committed with Unix style newlines, but they get checked out as they exist in the repo.

What about .bat and binary files? When this settings causes a conversion, Git issues a warning for me, so if it were to happen on a file where it should not, I'd get a warning and could fix the situation for that commit. I don't remember ever having had a problem with this, however, but my Git repos are pretty much exclusively portable source code and text files where I really, really want LF endings.

I think this is a nice setting if you're working on a Linux- or Mac-oriented project from a Windows PC.

autocrlf=true: I really always want CRLF in my checked out files

I mention this one for completeness, but would generally advise against it. If you always want your files with CRLF line endings when you're working on them, setting autocrlf to true will do that, but commit them in Unix-style LF line endings. This makes sense in some cases, but it can cause trouble in many cases.

Make your choice and save it globally

From the message you got, I'm guessing true was your setting, so you probably want to decide which of false or input is best for you, and set that globally, by running one of these two commands in your Git Bash prompt:

git config --global core.autocrlf false

or

git config --global core.autocrlf input

Wish-list for Git developers:

What I would really like is an autocrlf=prompt setting: ask me what to do any time I try to commit a file with a CRLF in it. I guess I should make myself a pre-commit hook for it...

joanis
  • 10,635
  • 14
  • 30
  • 40
  • `core.autocrlf` should *always* be set to false. For specific files, you would set `text eol` attributes, like in my [project .gitattributes](https://github.com/VonC/gitcred/blob/01d44899b82f8fcd5754a237328e2afbc6a03626/.gitattributes), where Windows bat script files should always used CRLF, and Go source files should always use LF. – VonC Jun 13 '22 at 14:53
  • @VonC There are good arguments for always using false, but I disagree that this must be a hard rule. Given that Git warns me every time `input` causes a conversion to happen, those cases where it could cause harm don't happen silently, I see them and I can do something about them. – joanis Jun 13 '22 at 17:26
  • I'm using Git inside VSCode to commit, not on the command line. I don't know how to change the option git uses. How do I change it inside VSCode ? – Data T Jun 14 '22 at 01:45
  • 1
    @DataT according to https://code.visualstudio.com/docs/editor/versioncontrol you can run those commands we've shown in the terminal window inside VSCode. The Git plugin is normally just a wrapper over Git CLI, and it should use the same configuration files. – joanis Jun 14 '22 at 12:36
0

If you're using Windows
The fix is by using autocrlf true

git config --global core.autocrlf true

You won’t see it work unless you delete and clone the repos again or restart the index by

git add --renormalize .

0

In VS Code, you will see an End of Line (EOL) indicator in the bottom right of your screen that shows "LF". Click on it and a selection will pop up in the top of the screen (where you select interpreter, etc), and you can change the value there as described on this site: https://dev.to/wagslane/how-to-get-consistent-line-breaks-in-vs-code-lf-vs-crlf-2c3p#:~:text=The%20Quick%20Fix&text=At%20the%20bottom%20right%20of,has%20the%20correct%20line%20breaks..

Kristy
  • 1