Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git makes all checked out files' end of line CRLF

I am programming on mac, and I don't really understand what Git does with the end of line of my files :

I created a repository with some files in Unix format (LF end of line).

When I clone the repository that I created, all my end of lines are CRLF. Shouldn't it detect automatically that I need LF end of line ?

I have autoclrf set to true.

GIT's documentation about autoclrf is pretty hard to understand :

If you simply want to have CRLF line endings in your working directory regardless of the repository you are working with, you can set the config variable "core.autocrlf" without changing any attributes.

[core]

   autocrlf = true

This does not force normalization of all text files, but does ensure that text files that you introduce to the repository have their line endings normalized to LF when they are added, and that files that are already normalized in the repository stay normalized.

The first sentence says "if you want to have all crlf", when the second sentence says that git will auto-adjust the end of lines.

In my case, it seems like Git converts everything to CRLF and leaves it like that when I try to clone.

like image 388
Antonin Avatar asked Sep 01 '10 18:09

Antonin


People also ask

How do I change from CRLF to LF in git?

text eol=crlf Git will always convert line endings to CRLF on checkout. You should use this for files that must keep CRLF endings, even on OSX or Linux. text eol=lf Git will always convert line endings to LF on checkout. You should use this for files that must keep LF endings, even on Windows.

How do I change the end of line from CRLF to LF?

Once you select View > Show Symbol > Show End of Line you can see the CR LF characters visually. You can then use the menu item Edit > EOL Conversion and select Unix (LF). After selection Edit > EOL Conversion > Unix (LF) your file will be correct for submission.

What is CRLF in git?

LF. original (usually LF , or CRLF if you're viewing a file you created on Windows) Both of these options enable automatic line ending normalization for text files, with one minor difference: core. autocrlf=true converts files to CRLF on checkout from the repo to the working tree, while core.


1 Answers

The gitattributes manpage is poorly laid out. In a later section, you'll find:

The core.eol configuration variable controls which line endings git will use for normalized files in your working directory; the default is to use the native line ending for your platform, or CRLF if core.autocrlf is set.

So, unless you've specified core.eol, you'll end up with lines terminated by CR+LF characters regardless of whether you're using Apple Mac OS X, Microsoft Windows, or Ubuntu Linux.

From your question:

The first sentence says "if you want to have all crlf", when the second sentence says that git will auto-adjust the end of lines.

It's important to note that there are two directions of adjustment that get performed when core.autocrlf is set to true:

  • CR+LFs will become LFs in your repository/repositories. That is to say, the commit files and repository back-end will have LFs at the end of lines in text files. This keeps things consistent in your commit history, making diffs/comparisons easier if one of your coworkers' IDEs decides to magically convert your LFs to CR+LFs (who wants to see that in their diff?). You'll save a few bytes of hard drive space as well, I suppose.
  • LFs will become CR+LFs in your working directory. In your checked out file system, any new text file will have lines ending in CR+LF once git touches it. This will happen even if the file had lines ending in plain LFs when you first created it.

The first thing you'll want to do is to unset core.autocrlf or set it to false. If you then want checked out text files to conform to the user's OS-preferred line endings, regardless of how they were created, just add this to your .gitattributes:

* text=auto

Alternatively, if git's not good at guessing which of your files are text, you could declare a specific extension to undergo this two-way normalization:

*.ext text

(where ext is the file extension in question)

like image 94
Justin Turner Arthur Avatar answered Oct 22 '22 08:10

Justin Turner Arthur