64

When I try to commit some changed files, I get the following error message with TortoiseGit

fatal: LF would be replaced by CRLF in <some file in the repo>

Now, before I get the usual LF vs CRLF answers, I know and understand what the debate is about. Secondly, I've also set my global settings to:

core.autocrlf true

Third, I've got a .gitattributes file.

So I -want- to make sure or files are forced to have CRLF.

What I don't understand is that it's saying FATAL and blocking me from continuing. A Warning? Sure! Do I know what I'm trying to do? I do!

I just want it to convert silently and STFU.

Alternatively, if it's forced to BLOCK me, is there a way I can update all files in the repo to be CRLF, so this warning is no longer issued?

These repos are private, so they will never be developed outside of Windows + Visual Studio.

How can I proceed?

halfer
  • 18,701
  • 13
  • 79
  • 158
Pure.Krome
  • 78,923
  • 102
  • 356
  • 586

6 Answers6

76

You might want to set core.safecrlf to "warn", if you want only warning and not a fatal error.

From "git config" mange page:

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.

CRLF conversion bears a slight chance of corrupting data.
When it is enabled, git will convert CRLF to LF during commit and LF to CRLF during checkout.
A file that contains a mixture of LF and CRLF before the commit cannot be recreated by git.
For text files this is the right thing to do: it corrects line endings such that we have only LF line endings in the repository.
But for binary files that are accidentally classified as text the conversion can corrupt data.

If you recognize such corruption early you can easily fix it by setting the conversion type explicitly in .gitattributes.
Right after committing you still have the original file in your work tree and this file is not yet corrupted. You can explicitly tell git that this file is binary and git will handle the file appropriately.

Unfortunately, the desired effect of cleaning up text files with mixed line endings and the undesired effect of corrupting binary files cannot be distinguished.
In both cases CRLFs are removed in an irreversible way. For text files this is the right thing to do because CRLFs are line endings, while for binary files converting CRLFs corrupts data.

I prefer identifying the exact files or types of file I want to force the eol with .gitattributes files only (with core.eol settings, which you have), and leave autocrlf to false.

In case of text fiels with mixed eol, this blog post suggests, for instance, to:

If you have Notepad++ installed in your computer, simply follow these steps.

  1. Open the file that is having the Fatal issue.
  2. Click Edit -> EOL Conversion then select Windows Format or to any that you’re having an issue committing.

Warning, if you have Git 2.17 or 2.18: a regression introduced in 8462ff4 ("convert_to_git(): safe_crlf/checksafe becomes int conv_flags", 2018-01-13, Git 2.17.0) back in Git 2.17 cycle caused autocrlf rewrites to produce a warning message despite setting safecrlf=false.

See commit 6cb0912 (04 Jun 2018) by Anthony Sottile (asottile).
(Merged by Junio C Hamano -- gitster -- in commit 8063ff9, 28 Jun 2018)

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • 1
    Very nice answer! Questions: 1. Why does git want to store LF's in the commit (ie. converting CRLF -> LF and vice versa on the otherway)? I actually thought it was doing the reverse *blush* ... saving LF's as CRLF. (I also grok that Windows are the freaks here with CRLF EOL's while other OS's are *generally* LF). 2. If i set AutoCrlF to false .. what would a sample line in `.gitattributes` be ? Exactly what I've got? So my `.gitattributes` file would correct convert? – Pure.Krome Mar 18 '13 at 08:01
  • 1
    @Pure.Krome 1. git by default would convert eol to respect to OS it was desinged to run on in the beginning: Linux. 2. if `core.autocrl` is set to false, then you can control the eol conversion with `core.eol`, so yes, your `.gitattributes` file looks fine, you just need to test it/complete it to cover the files you actually want to convert. – VonC Mar 18 '13 at 08:04
  • 1
    I got this problem when letting pgAdmin save to a file i was writing with notepad++. Just as a heads up. – Love and peace - Joe Codeswell Jul 17 '15 at 13:09
33
git config --global core.safecrlf false
Snowcrash
  • 66,400
  • 60
  • 203
  • 323
12

This will disable the crlf fatal warning.

git config core.autocrlf false
git config core.safecrlf false
Wireblue
  • 1,219
  • 1
  • 12
  • 24
sanjeet bisht
  • 321
  • 3
  • 8
4

Since your repo is private, you can set git-config like this:

git config --global core.autocrlf false

This will solve your problem.If you have any further question, You can read the 《Pro git》:

If you’re a Windows programmer doing a Windows-only project, then you can turn off this functionality, recording the carriage returns in the repository by setting the config value to false: $ git config --global core.autocrlf false

But when collaborating, you should better do these below:

  1. add .gitattributes, Github help-Dealing with line endings will be helpful.
  2. git config --global core.safecrlf true
    • windows:git config --global core.autocrlf true
    • mac or linux:git config --global core.autocrlf input

You may want to Read Git docs-git config for more info.

Wade
  • 61
  • 4
1

git config --global core.autocrlf false will checkin files with CRLF, that is not used to.

I've noticed on Windows, that with core.autocrlf true git doesn't like files with LF and core.autocrlf input doesn't like CRLF.

So: commit CRLF files with core.autocrlf true and LF files with core.autocrlf input (or convert them to CRLF).

Usually files with LF is autogenerated by code generators (e.g. https://start.spring.io or http://yeoman.io/)

Grigory Kislin
  • 12,805
  • 7
  • 98
  • 154
0

With .gitattributes file, use

*.h text=auto
*.cpp text=auto
*.txt text=auto

as is documented in https://git-scm.com/docs/gitattributes.

MyGeertRo
  • 111
  • 1
  • 3