Line endings
How to configure line endings for your repo and to get them consistent
If you or your team are working on multiple operating systems, this can cause issues. Such as warnings from Git, and files appearing modified even though they look the same aside from whitespace.
See also this article - CRLF vs. LF: Normalizing Line Endings in Git
Line endings
You can edit files and commit then using one of the following line endings:
OS | Abbreviation | Full name | Character |
---|---|---|---|
Unix-style | LF | “line feed” | \n |
Windows style | CRLF | “carriage return and line feed” | \r\n |
You might also see “CR” or carriage return as the old style Windows line ending, but this is not an option in Git.
Vertify ling endings
$ git ls-files --eol
e.g.
i/lf w/lf attr/text=auto .editorconfig
i/lf w/lf attr/text=auto .gitignore
i/lf w/lf attr/text=auto README.md
...
You can also pass a path to that.w
How to fix inconsisten line endings
You might get a message from git status
like this: “LF will be replaced by CRLF”. And if you check your git diff
you might see ^M
throughout the file at the end of each line.
To solve this, you can change how Git behaves (see next section). Or hange your IDE’s LF/CRLF setting.
Advice below is based on this StackOverflow thread.
See core.autocrlf in the Git docs for what the settings values mean.
Set input Auto-CRLF
No input conversion is done.
I found this works for Linux and macOS.
$ git config --global core.autocrlf input
Set Auto-CRLF on Windows
This will make sure that when you checkout in Windows, all LF
will be converted to CRLF
.
$ git config --global core.autocrlf true
See more info on StackOverflow thread.
The only specific reasons to set
autocrlf
totrue
are:
- avoid git status showing all your files as modified because of the automatic EOL conversion done when cloning a Unix-based EOL Git repo to a Windows one (see issue 83 for instance)
- and your coding tools somehow depends on a native EOL style being present in your file
Set git attributes
.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.
e.g.
* 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
If you still see on Windows that Git doesn’t change the files correctly, you can set this in .editorconfig
.
root = true
[*]
end_of_line = lf
Normalize line endings in all files
After setting core.autocrlf
in Git config or the text
attribute in .gitattributes
, set this.
$ git add --renormalize .
Based on StackOverflow thread and article.
See add –renormalize in the Git docs.