Git and CR vs LF (but not CRLF)

This may seem like an unnecessary question (and maybe it is a question of redundancy), but I can not find the answer. Here's the situation:

My application creates text files with CR as line endings. More specifically, I do not set the final lines in CR, it is just the result of the command that I use to get the body of the text. Of course, I could manually convert CR to LF, but I do not want to avoid this.

Git treat these files as a single line (for example, during diff). I determined through this test repository that the reason for the line ending is: https://github.com/jfletcher4d/diff-test

I'm really not interested in what lines are enclosed in the file system. This is not important, at least for now (I will probably in the end take care if I need to import these files, right now this is just an export). But I do not want to convert CR to LF in my application, if I can avoid it, for performance reasons, as well as for reasons of anal retentivity :) Ie this is not a question of how to create text files, but rather, how to force each text file in repo only have LF.

Can git be configured to change line endings all to LF, even if the files were committed with CR in them?

I am on Windows using TortoiseGit and msysgit (and a bit of TortoiseSVN on the side) interchangeably while I learn git, if that matters.

+8
git newline lf
source share
2 answers

Git does not seem to support CR line endings, so I would like to write filter to convert newlines. Files in the work tree will have a CR line ending, and they will be transparently converted to LF when they are indexed. The filter consists of two parts: "clean" checks the files, and "smudge" checks the files.

Use this in .git/config :

 [filter "cr"] clean = tr '\\r' '\\n' smudge = tr '\\n' '\\r' 

And .git/info/attributes (or .gitattributes if it should be a version)

 * filter=cr 

Please note that this automatically makes git-diff happy, as it will use the "clean" version.

Do not forget to install the template only in the files you need, or the binary files will be damaged, and all text files will be checked with the end of the CR line.

Also note that if the filter is not configured, it will fail, so add a configuration line when setting up a new copy of the repository.

+10
source share

FWIW, I finished converting line endings to LF on the local side of things. This turned out to be a simpler / less buggy solution.

+2
source share

All Articles