2

So, I have this new repo where I'm trying to start a collaborative project. I have already pushed .gitignore and .gitattributes (handling auto-crlf) files to it.

My .gitattributes file is:

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
*.mdj binary

I created a repository on GitHub and cloned it to my PC via SourceTree. Now, I'm trying to create a new CLion project inside it, but when I try to add CMake and main.c files to be committed, I get an LF to CRLF error:

The following problems have occurred when adding the files:
fatal: LF would be replaced by CRLF in tpCuat/CMakeLists.txt
 during executing git "C:\Program Files\Git\cmd\git.exe" -c core.quotedpath=false add --ignore-errors -- tpCuat/CMakeLists.txt

The thing is, these files are created in Windows by me (CLion, actually), so I can't see why this error is coming up.

Edward Thomson
  • 63,947
  • 11
  • 134
  • 169

1 Answers1

2

This warning (or error) means exactly that: you have a linefeed in your file (\n, ASCII 10), and you have a configuration that tells Git that it should do CRLF conversions. In this case, it's your .gitattributes:

* text=auto

This is a problem because Git is telling you that it cannot provide you a guarantee that the file you're putting in will be the file that you later get out.

When you add this file to your repository (git add), the text=auto attribute tells Git to convert all CRLF (\r\n, ASCII 13 followed by ASCII 10) in your file to a bare linefeed (\n, ASCII 10) when it stores it in the repository. When Git subsequently tries to put the file on disk (git checkout) then it will convert all the linefeeds (\n) in the file to CRLF (\r\n) as it writes it.

When you put a bare linefeed into a file but tell Git to do CRLF conversion, it can't round-trip this. Consider if you have some file (line endings illustrated):

line one\r\n
line two\r\n
line three accidentally ends with a newline!\n
line four\r\n

Now when Git adds this to the repository, it will do CRLF conversion and store:

line one\n
line two\n
line three accidentally ends with a newline!\n
line four\n

And when it goes to check it out again:

line one\r\n
line two\r\n
line three accidentally ends with a newline!\r\n
line four\r\n

Notice how line three now ends with a CRLF (\r\n) when it didn't in the original file? That's what Git is warning you about here. It's telling you that it can't possibly accurately give you back what you're putting in.

If this is a problem for you, then you should turn on core.safecrlf so that Git will error when this happens and require you to fix your line ending issues. Otherwise, if you don't care, you can safely ignore this warning.

Why is CLion doing something silly like putting a bare linefeed into your file at all? Well, that's another question entirely!

Edward Thomson
  • 63,947
  • 11
  • 134
  • 169