How to change line-ending settings

Solution 1:

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.

Solution 2:

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. Using .gitattributes file

Global Configuration

In Linux/OSX

git config --global core.autocrlf input

This will fix any CRLF to LF when you commit.

In Windows

git config --global core.autocrlf true

This will make sure that, when you checkout in windows, all LF will be converted 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 to set their own config. This file should be placed in the repository root and. If it exists, 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 you want to specify the line ending explicitly, you can use:

* text eol=crlf
* text eol=lf

The first one is for checkout and the second one is for commit.

*.jpg binary

This will treat all .jpg images as binary files, regardless of path. So no conversion needed.

Or you can add path qualifiers:

my_path/**/*.jpg binary

Solution 3:

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?

Solution 4:

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.