Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git * text=auto in the gitattributes file and line endings

Based on this post: What is the purpose of `text=auto` in `.gitattributes` file? line endings are converted to LF for text files if you have the following in the .gitattributes file:

* text=auto

I just tested this on a local repository:

$ git add -A
warning: LF will be replaced by CRLF in [bla]/.gitattributes.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in [bla]/.gitignore.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in [bla]/README.md.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in [bla].csproj.
The file will have its original line endings in your working directory.
warning: LF will be replaced by CRLF in 

But there it says the it will convert to CRLF. In the post above it says it will convert to LF which is not the case in this test.

So it seems that:

* text=auto

will be converting to the line ending type based on the OS (CRLF for windows and LF for linux). But that is not what is being described here:

https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

Based on below comments/answers it seems that the warning resulting from having this:

* text=auto

in the .gitattributes file:

warning: LF will be replaced by CRLF in [bla]/README.md.
The file will have its original line endings in your working directory.

Actually means that when you do a check-out (next time you checkout a file from the repository to you working directory) text files currently with LF endings will be converted to have CRLF.

The warning does NOT address that on check-in lines will have LF endings which is what the documentation says here:

https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

Set to string value "auto" When text is set to "auto", the path is marked for automatic end-of-line normalization. If Git decides that the content is text, its line endings are normalized to LF on checkin.

like image 243
u123 Avatar asked Nov 23 '15 13:11

u123


1 Answers

This message is a bit confusing.

Git will warn you whenever it is not going to round-trip your file identically with your current line ending conversion settings. This warning is not because Git is going to put CRLFs into the repository (it's not) - this warning comes because the file that Git would checkout is not the same as the file that's currently on your disk.

For whatever reason, the files in your working directory have Unix style line endings (or a mix of Unix and Windows-style). You should be able to see this with a hex editor. For example, I have a file with Unix style line endings:

C:\Temp>hexdump /C foo
00000000  68 65 6c 6c 6f 21 0a                              |hello!.|
00000007

If I add the file to my repository (with * text=auto or core.autocrlf=true):

C:\Temp>git add foo
warning: LF will be replaced by CRLF in foo.
The file will have its original line endings in your working directory.

As git indicates, my file in my current working directory has its original (Unix-style) line endings:

C:\Temp>hexdump /C foo
00000000  68 65 6c 6c 6f 21 0a                              |hello!.|
00000007   

But the file in the repository also has Unix style line endings:

C:\Temp>git ls-files --stage
100644 4effa19f4f75f846c3229b9dbdbad14eff362f32 0       foo

C:\Temp>git cat-file blob 4effa19 | hexdump /C
00000000  68 65 6c 6c 6f 21 0a                              |hello!.|
00000007

But if I ask git to create the file contents then it will create a different file - one with CRLF line endings, which is what that warning actually indicates:

C:\Temp>del foo

C:\Temp>git checkout -f foo

C:\Temp>hexdump -C foo
00000000  68 65 6c 6c 6f 21 0d 0a                           |hello!..|
00000008

So this message is just to warn you that the next checkout of this file will not actually match what you have on your disk currently. In this case, this may be harmless, but this would be critically bad if you were adding a file where the line ending configuration was critical that it matched.

like image 198
Edward Thomson Avatar answered Oct 02 '22 03:10

Edward Thomson