Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git with autocrlf=true checks out files with mixed line endings as-is

So, I always thought that with core.autocrlf=true Git replaces all LF endings with CRLF when checking out the file into the working directory.

From the Git book:

If you’re on a Windows machine, set it to true – this converts LF endings into CRLF when you check out code

However, when checking out a file with mixed line endings and core.autocrlf set to true, my version of Git checks out the file as-is.

I've found a very convenient GitHub repository to test this behaviour - https://github.com/YueLinHo/TestAutoCrlf

Test results:

  • A file with LF endings only (LF.txt)
    • With autocrlf=false: checked out as-is (all line endings are LF)
    • With autocrlf=true: all line endings are changed to CRLF on checkout

So far so good, everything as I expected. Now for the file with mixed line endings:

  • A file with mixed line endings (MIX-more_CRLF.txt, MIX-more_LF.txt)
    • With autocrlf=false: checked out as-is (a mix of LF and CRLF)
    • With autocrlf=true: checked out as-is (a mix of LF and CRLF)

Why does this happen? I haven't seen anything about autocrlf=true not touching files with mixed line endings.

Are my Git settings at fault? I checked the core.autocrlf setting running git config --get core.autocrlf in the repository folder after checking out with autocrlf=true in the global .gitconfig, and the command returned true. There is no .gitattributes file to overwrite the settings.

All tests were made on Git version 1.9.5.msysgit.0.

EDIT: Same behaviour on the latest msysgit version 1.9.5.msysgit.1.

My original problem is that I somehow managed to commit a mixed-line ending file with only LF endings while having core.autocrlf set to true, meaning the file was checked out as-is, but commited with CRLF changed to LF. I'm currently working from another machine and cannot reproduce this behaviour on my msysgit version.

like image 504
sdds Avatar asked Apr 22 '15 09:04

sdds


People also ask

What does Autocrlf true do?

autocrlf = true This means that Git will process all text files and make sure that CRLF is replaced with LF when writing that file to the object database and turn all LF back into CRLF when writing out into the working directory.

What is Autocrlf in git?

autocrlf command is used to change how Git handles line endings. It takes a single argument. On macOS, you simply pass input to the configuration. For example: $ git config --global core.autocrlf input # Configure Git to ensure line endings in files you checkout are correct for macOS.

How do I know if a file is LF or CRLF?

use a text editor like notepad++ that can help you with understanding the line ends. It will show you the line end formats used as either Unix(LF) or Macintosh(CR) or Windows(CR LF) on the task bar of the tool. you can also go to View->Show Symbol->Show End Of Line to display the line ends as LF/ CR LF/CR.


2 Answers

I am reposting an answer that was deleted by its owner, because I think it gives the best explanation. I have no idea why the author deleted it, I think it's correct and I've voted to undelete.

Apparently this behavior is hardcoded in Git and does not depend on core.safecrlf (and I've tested this, mixed files are left untouched even if I set git config core.safecrlf false.

Original answer follows:


Autocrlf doesn't convert mixed line endings, as the git's source code tells:

https://github.com/git/git/commit/a0ad53c18100226cb1a138cb9b3bc3615170be8f

Note the comments here:

/* No "naked" LF? Nothing to convert, regardless. */

and

/* If we have any CR or CRLF line endings, we do not touch it */
/* This is the new safer autocrlf-handling */

Mixed line endings conversion is non-reversible, when it's done, Git crashed.

So, if you wanna convert your files' line endings automatically, it might be a good idea to set a .gitattributes file dealing with end-of-line. Such as:

LF.txt eol=lf
CRLF.txt eol=crlf
like image 115
sashoalm Avatar answered Sep 17 '22 14:09

sashoalm


What is the value of core.safecrlf ?

If core.safecrlf is set to true, then mixed line endings files will not be converted. (Because the converting is not reversible if line endings is mixed)

like image 22
xdazz Avatar answered Sep 17 '22 14:09

xdazz