Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git on Windows: What do the crlf settings mean?

I don't understand the complexities related to CrLf settings in git: core.autocrlf, core.safecrlf

I'm developing a cross-platform project in a team and would like both Windows and Linux developers to be able to work together without git marking files as modified just because of line ending style.

What do the various settings mean? What would be the consequences of choosing any of the options? And what would be the best solution for my case?

Yes, I'm aware of this question and the answers there were not insightful, thus not helpful.

like image 954
Jonathan Livni Avatar asked Nov 15 '10 06:11

Jonathan Livni


People also ask

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.

Does Windows use CRLF or LF?

Back to line endings The reasons don't matter: Windows chose the CR/LF model, while Linux uses the \n model. So, when you create a file on one system and use it on the other, hilarity ensues.

How do I know if my 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.

What does auto CRLF do?

core.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.


2 Answers

The three values for autocrlf:

  • true - when content goes into the repository (is committed), its line endings will be converted to LF, and when content comes out of the repository (is checked out), the line endings be converted to CRLF. This is in general meant for clueless windows users/editors. Given the assumption that an editor (or user) is going to create files with CRLF endings, and will freak out if it sees normal LF endings, but that you want LF endings in the repo, this will hopefully cover you. It's possible for things to go awry, though. There are examples of spurious merge conflicts and reports of modified files in the linked questions.

  • input - when content goes into the repository, its line endings will be converted to LF, but content is left untouched on the way out. This is basically in the same realm as true, with the assumption that the editors actually can deal with LF endings correctly; you're just guarding against the possibility of accidentally creating a file with CRLF endings.

  • false - git doesn't deal with line endings at all. It's up to you. This is what a lot of people recommend. With this setting, if a file's line endings are going to be messed with, you'll have to be aware of it, so merge conflicts are a lot less likely (assuming informed users). Educating developers about how to use their editors/IDEs can pretty much take care of the problem. All of the editors I've seen designed for programmers are capable of dealing with this if configured properly.

Note that autocrlf will not affect content which is already in the repository. If you've committed something with CRLF endings previously, they'll stay that way. This is a very good reason to avoid depending on autocrlf; if one user doesn't have it set, they can get content with CRLF endings into the repo, and it'll stick around. A stronger way to force normalization is with the text attribute; setting it to auto for a given path will mark it for end-of-line normalization, assuming git decides the content is text (not binary).

A related option is safecrlf, which is basically just a way to make sure you don't irreversably perform CRLF conversion on a binary file.

I don't have a ton of experience dealing with Windows issues and git, so feedback about implications/pitfalls is certainly welcome.

like image 162
Cascabel Avatar answered Oct 02 '22 14:10

Cascabel


I explored 3 possible values for commit and checkout cases and this is the resulting table:

╔═══════════════╦══════════════╦══════════════╦══════════════╗ ║ core.autocrlf ║     false    ║     input    ║     true     ║ ╠═══════════════╬══════════════╬══════════════╬══════════════╣ ║   git commit  ║ LF => LF     ║ LF => LF     ║ LF => LF     ║ ║               ║ CR => CR     ║ CR => CR     ║ CR => CR     ║ ║               ║ CRLF => CRLF ║ CRLF => LF   ║ CRLF => LF   ║ ╠═══════════════╬══════════════╬══════════════╬══════════════╣ ║  git checkout ║ LF => LF     ║ LF => LF     ║ LF => CRLF   ║ ║               ║ CR => CR     ║ CR => CR     ║ CR => CR     ║ ║               ║ CRLF => CRLF ║ CRLF => CRLF ║ CRLF => CRLF ║ ╚═══════════════╩══════════════╩══════════════╩══════════════╝ 

I would recommend to use core.autocrlf = input across all platforms. In this case if Git faces CRLF it will implicitly convert it into LF, and existing files with LF remain as is.

like image 41
pratt Avatar answered Oct 02 '22 14:10

pratt