704
votes

Is there a file or menu that will let me change the settings on how to deal with line endings?

I read there are 3 options:

  1. Checkout Windows-style, commit Unix-style

    Git will convert LF to CRLF when checking out text files. When committing text files, CRLF will be converted to LF. For cross-platform projects, this is the recommended setting on Windows ("core.autocrlf" is set to "true")

  2. Checkout as-is, commit Unix-style

    Git will not perform any conversion when checking out text files. When committing text files, CRLF will be converted to LF. For cross-platform projects this is the recommended setting on Unix ("core.autocrlf" is set to "input").

  3. Checkout as-is, commit as-is

    Git will not perform any conversions when checking out or committing text files. Choosing this option is not recommended for cross-platform projects ("core.autocrlf" is set to "false")

7
Which of these is the default?Stephen
Nevermind it looks like the default is true, which I think is appropriate.Stephen
I actually find that the 3-rd option works better. Otherwise I often have been in situations when I edit both batch and sh scripts on the same platform (Windows/Linux) and then commit them and Git automatically "fixes" line endings for one platform... No, I prefer to be self-conscious about line endings and commit/checkout them exactly as they are.JustAMartin
@Neutrino I wish this was true, but one example of IDE that messes with your line endings (and doesn't offer a reasonable configuration option to turn this off) is Visual Studio.Cássio Renan

7 Answers

638
votes

The normal way to control this is with git config

For example

git config --global core.autocrlf true

For details, scroll down in this link to Pro Git to the section named "core.autocrlf"


If you want to know what file this is saved in, you can run the command:

git config --global --edit

and the git global config file should open in a text editor, and you can see where that file was loaded from.

212
votes

Line ending format used in OS

  • Windows: CR (Carriage Return \r) and LF (LineFeed \n) pair
  • OSX,Linux: LF (LineFeed \n)

We can configure git to auto-correct line ending formats for each OS in two ways.

  1. Git Global configuration
  2. Use .gitattributes file

Global Configuration

git config --global core.autocrlf input

This will fix any CRLF to LF when you commit.

git config --global core.autocrlf true

This will make sure when you checkout in windows, all LF will convert to CRLF

.gitattributes File

It is a good idea to keep a .gitattributes file as we don't want to expect everyone in our team set their config. This file should keep in repo's root path and if exist one, git will respect it.

* text=auto

This will treat all files as text files and convert to OS's line ending on checkout and back to LF on commit automatically. If wanted to tell explicitly, then use

* text eol=crlf
* text eol=lf

First one is for checkout and second one is for commit.

*.jpg binary

Treat all .jpg images as binary files, regardless of path. So no conversion needed.

Or you can add path qualifiers:

my_path/**/*.jpg binary
38
votes

For a repository setting solution, that can be redistributed to all developers, check out the text attribute in the .gitattributes file. This way, developers dont have to manually set their own line endings on the repository, and because different repositories can have different line ending styles, global core.autocrlf is not the best, at least in my opinion.

For example unsetting this attribute on a given path [. - text] will force git not to touch line endings when checking in and checking out. In my opinion, this is the best behavior, as most modern text editors can handle both type of line endings. Also, if you as a developer still want to do line ending conversion when checking in, you can still set the path to match certain files or set the eol attribute (in .gitattributes) on your repository.

Also check out this related post, which describes .gitattributes file and text attribute in more detail: What's the best CRLF (carriage return, line feed) handling strategy with Git?

7
votes

For me what did the trick was running the command

git config auto.crlf false

inside the folder of the project, I wanted it specifically for one project.

That command changed the file in path {project_name}/.git/config (fyi .git is a hidden folder) by adding the lines

[auto]
    crlf = false

at the end of the file. I suppose changing the file does the same trick as well.

1
votes

If you want to convert back the file formats which have been changed to UNIX Format from PC format.

(1)You need to reinstall tortoise GIT and in the "Line Ending Conversion" Section make sure that you have selected "Check out as is - Check in as is"option.

(2)and keep the remaining configurations as it is.

(3)once installation is done

(4)write all the file extensions which are converted to UNIX format into a text file (extensions.txt).

ex:*.dsp
   *.dsw

(5) copy the file into your clone Run the following command in GITBASH

while read -r a;
do
find . -type f -name "$a" -exec dos2unix {} \;
done<extension.txt
1
votes

The .gitattributes file

The easiest way is to use a local .gitattributes file in your repo.

You can also change line-endings for specific file extensions too

*           text=auto     # auto
*.txt       text
*.vcproj    text eol=crlf # windows line-endings
*.sh        text eol=lf   # linux line-endings
*.jpg       -text

This also overrides the global defaults, so it's much more portable and makes the repo more reliable on different machines.

0
votes

Below steps work for me

Add a git attributes on root project folder (it will be useful for upcoming file)

* text=auto

*.tf  eol=lf
*.tfvars  eol=lf
*.yml  eol=lf

After execute below command for give a support to already exist files

find ./ -type f \( -iname \*.tf -o -iname \*.tfvars -o -iname \*.md -o -iname \*.yml \) -print0 | xargs -0 dos2unix

dos2unix .gitignore
dos2unix .gitattributes