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.autocrlfis a global or repository setting that is configured by
git configcommand. The configuration file where
core.autocrlfis set into can not be committed into the repository.
Git attributes is a per-repository setting. The attributes are written in a file named
.gitattributethat 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 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)
Therefore test that on your machine and decide how to set
Below description from git config about
trueif you want to have
CRLFline endings in your working directory and the repository has LF line endings.
# 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
Git configuration file can not committed into the repository, but
.gitattributesfile can be committed.
Not sure which value to use
If you have a new repository, just set
core.autocrlfto input or use git attribute
eoland set it to
lfintroduced below no matter the OS you are working on (even Windows).
Git attributes –
eol are two attributes that can help to set your line ending style in
Git will handle the files in whatever way it thinks is best. This is a good default option.
Git will always convert line endings to
CRLFon checkout. You should use this for files that must keep
CRLFendings, even on OSX or Linux.
Git will always convert line endings to
LFon checkout. You should use this for files that must keep LF endings, even on Windows.
Git will understand that the files specified are not text, and it should not try to change them. The
binarysetting is also an alias for
Below description in .gitattributes about
textto string value
textis 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.
eolto string value
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
.gitattributesfile 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
# 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"