Line ending configuration in Git

When you work with people on different OS systems, you probably run into line-ending (CRLF or LF) issues. Git provides different ways to control line ending style to help you with this issue. Most content is organized from Configuring Git to handle line endings and new content is added for comprehensive understanding.

For a quick view, there are two ways to set your line ending style:

  • core.autocrlf is a global or repository setting that is configured by git config command. The configuration file where core.autocrlf is set into can not be committed into the repository.

  • Git attributes is a per-repository setting. The attributes are written in a file named .gitattribute that can be committed into the repository and overrides core.autocrlf, ensure an consistent behavior of different users regardless of their Git settings.

Git configuration variable – core.autocrlf

core.autocrlf is a Git configuration variable that you can set with git config for all repositories or for a specific repository. It can be set with one of the three following values: true, input and false.

  • true (If you want CRLF line ending in repository and working directory even on Linux)

    Let Git to handle the files in whatever way it thinks is best.

    On Windows (Git v2.13.2):

    LF to CRLF on commit.

    LF to CRLF on checkout if the file does not exist in the working directory yet.

    $ git config core.autocrlf true
    
    $ git add eol-lf.txt
    warning: LF will be replaced by CRLF in eol-lf.txt.
    The file will have its original line endings in your working directory.
    
  • input (If you want LF line ending in repository and working directory even on Windows)

    CRLF to LF on commit.

    CRLF to LF on checkout if the file does not exist in the working directory yet.

    $ git config core.autocrlf input
    
    $ git add eol-crlf.txt
    warning: CRLF will be replaced by LF in eol-crlf.txt.
    The file will have its original line endings in your working directory.
    
  • false (If you do not want conversion of line ending)

    No conversion.

Therefore test that on your machine and decide how to set core.autocrlf.

Note

Below description from git config about core.autocrlf is incorrect:

Set to true if you want to have CRLF line endings in your working directory and the repository has LF line endings.

Examples

# Set core.autocrlf to input globally
$ git config --global core.autocrlf input

# Config core.autocrlf to input locally for the current repository.
$ git config core.autocrlf input

Note:

Git configuration file can not committed into the repository, but .gitattributes file can be committed.

Not sure which value to use

If you have a new repository, just set core.autocrlf to input or use git attribute eol and set it to lf introduced below no matter the OS you are working on (even Windows).

Git attributes – text and eol

text and eol are two attributes that can help to set your line ending style in .gitattribute file.

  • text=auto

    Git will handle the files in whatever way it thinks is best. This is a good default option.

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

  • text eol=lf

    Git will always convert line endings to LF on checkout. You should use this for files that must keep LF endings, even on Windows.

  • binary

    Git will understand that the files specified are not text, and it should not try to change them. The binary setting is also an alias for -text -diff.

Note

Below description in .gitattributes about text and eol is incorrect:

Set text to string value auto

When text is set to “auto”, the path is marked for automatic end-of-line conversion. If Git decides that the content is text, its line endings are converted to LF on checkin. When the file has been committed with CRLF, no conversion is done.

Set eol to string value lf

This setting forces Git to normalize line endings to LF on checkin and prevents conversion to CRLF when the file is checked out.

Example from Configuring Git to handle line endings

Here’s an example .gitattributes file. You can use it as a template for your repositories:

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary

Note: A .gitattributes file can be located in any directory of a repository which takes effect to content of that folder and its sub folders.

Refresh a repository after changing line endings

If you have committed files with wrong CRLF/LF line endings. You can force to add the tracked files again to the index even their content is not modified after changing core.autocrl or text attribute.

# Suppose you have committed file with wrong CRLF/LF line endings.

# Now:
# Change `core.autocrlf` or `text` to use your desired line ending style.

# Execute `git add` with --renormalize option
$ git add --renormalize .
Show the rewritten, normalized files.

# Show what happed
$ git status

# Commit the changes to your repository.
$ git commit -m "Normalize all the line endings"

Resources

Configuring Git to handle line endings

core.autocrlf

Git attributes