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 Link to heading
The line ending on Windows is
CRLF, on Linux is
LF. To prevent the line ending issue, we can set
true on Windows to let git convert
LF when commit, and convert
CRLF when checkout. It is automatically configured if you install git on Windows.
# 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 Link to heading
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 Link to heading
* text=auto, Git handles the files in whatever way it thinks is best. This is a good default option.
*.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.
*.png binary to explicitly declare a file as binary, so Git does not convert it. (
binray is an alias for
Force conversion when checkout: Link to heading
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
lf. If it’s not specified, the line ending will be determined by
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 Link to heading
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 Link to heading
- Line endings in tarball also follows the
.gitattributes. It’s identical to Git checkout on Linux machine.
.gitattributessettings 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 use
git add --renormalize .to force Git to refresh all tracked files. For example, if the bat file has been add as
crlfin Git index and then you set it as
.gitattributes. Running this command asks Git change it to