Robin Hsu
Robin Hsu

Reputation: 4504

How to set system default line ending to CRLF mode for git in Cygwin?

Is there a way to set default line ending mode different from the system's (Cygwin's) default for git?

I use both Windos-Git (i.e. GitBash) and the native git on Cygwin. The root cause of my problem is that CYGWIN considers itself a un*x, and thus the default line ending is set to LF. However, CYGWIN runs on Windows, and most other cooperated software, including GitBash, want the line ending mode to be CRLF.

I use both because I administrate the git database, and need to assure usability of different git tools/clients for different engineers.

In Cygwin, When I set core.crlf to true, it won't convert text file to CR/LF for me, since Cygwin considers itself a un*x.

Is there a settings in GIT to tell git to force git to use CR/LF as it's 'auto' conversion default settings? -OR- Any solutions for this situation?

Upvotes: 3

Views: 3992

Answers (3)

Warren
Warren

Reputation: 11

Add SHELLOPTS igncr to your Windows environment variables. Worked for me.

Upvotes: 1

Robin Hsu
Robin Hsu

Reputation: 4504

After long experiments with the confusion of other causes (including file execution bit problem, of which I mistakenly thought as line ending problem), finally I got the same conclusion as VonC's answer. However, here I will post a more complete settings and explanations:

First of all, I will ask engineers to add following lines in their ~/gitconfig, in the [core] section:

[core]
    filemode = false
    autocrlf = false

This fixes the file execution mode problem (filemode option), and forces git not to automatically doing crlf translation (autocrlf option).

Secondly, for windows, we will may optionally use this option.

[core]
    eol = crlf

This tells git to translate file marked as 'text' in .gitattributes to CR/LF line ending, if their line ending mode is not specified in .gitattributes. Note that this option is used by git ONLY when autocrlf = false in .gitconfig file.

We let the engineers to decide to use eol = crlf or even eol = lf, since they knows what line ending mode they like.

Lastly, for project's .gitattributes, we first categorize our text files into three groups: (A) text files that needs to have CR/LF line endings, (B) text files that is generated by the tool/tool-chain (such as .xml files), which we won't edit them by text editors, (C) text files that is OK to use either line ending modes (such as .cpp, .c, .h files). Then we set our .gitattributes as follows:

*.<extension-for-groupA>    text    eol=crlf
*.<extension-for-groupB>    binary
*.<extension-for-groupC>    text

The third line is essential. If the file is not marked as 'text', git will treat different line endings as some difference for a file in git status.

The second line has no effect, if the user's .gitconfig have autocrlf = false. Yet for safety, we add this line.

An interesting setting is:

*.sh                 binary

For many regular shell scripts, use *.sh text eol=lf should be fine. However, those shell scripts with inline CR/LF special characters could be corrupted.


== UPDATE ==

Forgot to say, we still need to add/change fileMode = false in [core] section for remote repository, and/or local repository. This is essential. Local repository settings overrides users .gitconfig settings.

Upvotes: 5

VonC
VonC

Reputation: 1327384

Just to be clear, the gitattributes doc and "Dealing with line endings" recommend:

*.<yourExtension> text eol=crlf

There is no "core.crlf"

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.

Upvotes: 0

Related Questions