Line Ending in Git
Table of Contents
When working on a project with multiple developers, the line ending can be troublesome. This article will explain how to configure line ending in Git.
Basic configuration #
The line ending on Windows is CRLF
, on Linux is LF
. To prevent the line ending issue, we can set core.autocrlf
to true
on Windows to let git convert CRLF
to LF
when commit, and convert LF
to CRLF
when checkout. It is automatically configured if you install git on Windows.
Configuring Git to handle line endings - GitHub Docs
# Configure Git to ensure line endings in files you checkout are correct for Windows.
# For compatibility, line endings are converted to Unix style when you commit files.
$ git config --global core.autocrlf true
Advanced configuration #
You can also use .gitattributes
to control the line ending in each repository. The .gitattributes
file is a text file that tells Git how to handle files in the repository. You can specify the line ending of each file type in this file.
Auto convert line ending #
With * text=auto
, Git handles the files in whatever way it thinks is best. This is a good default option.
Use *.c text
to explicitly declare a file as a text file, so this file is always normalized and converted to native line endings on checkout.
Use *.png binary
to explicitly declare a file as binary, so Git does not convert it. (binray
is an alias for -text -diff
)
Force conversion when checkout: #
You can use eol
to force conversion when checkout. The following config enforces bat files to be converted to CRLF
when checkout even on Mac and Linux.
* text=auto
*.bat eol=crlf
This is the result of git ls-files --eol
on Windows and Linux:
git ls-files --eol src/azure-cli/az.bat
i/lf w/crlf attr/text=auto eol=crlf src/azure-cli/az.bat
i
means the index, w
means the working tree, attr
means the attribute used when checking out or committing.
You can set eof
to crlf
or lf
. If it’s not specified, the line ending will be determined by core.autocrlf
or core.eol
. If text
is set but neither of those variables are set, then the default value is crlf
on Windows and lf
on Linux and Mac.
Refresh setting #
If you change the .gitattributes
file, you need to run the following command to refresh the working tree.
# Please commit the .gitattributes changes before run this command.
git rm -rf --cached .
git reset --hard HEAD
Extra #
- Line endings in tarball also follows the
.gitattributes
. It’s identical to Git checkout on Linux machine. - The
.gitattributes
settings will only affect new commits. If you want to change the line ending of the files that already in the Git index after changing line ending settings, you can usegit add --renormalize .
to force Git to refresh all tracked files. For example, if the bat file has been add ascrlf
in Git index and then you set it astext
in.gitattributes
. Running this command asks Git change it tolf
in index.