All git repositories must have a .gitattributes
file at the root.
Using a .gitattributes
file effectively overrides the global core.autocrlf
git setting, ensuring that different environments don't exhibit different behavior with newlines.
This should be the first line of the .gitattributes
file:
* text=auto eol=lf
This causes git to change newlines from CRLF to LF when files are committed, ensuring that files on all platforms use the same newlines, and preventing files with mixed newlines from being committed.
Existing repositories with no .gitattributes
file or a .gitattributes
file that doesn't look like the above (e.g. with * -text
) may have files with CRLF and/or mixed line feeds.
Existing repositories should be changed to use LF and the .gitattributes
setting above.
Please note that changing CRLF to LF can adversely affect blame.
This can be mitigated by using a .git-blame-ignore-revs
file, which will cause GitHub to ignore those commits in the blame view.
The benefits of LF everywhere (and solving the problems of mixed-line endings) outweigh the costs of difficulties with blame.
To convert a repository, follow these steps:
- Commit your working tree.
- Add or change your
.gitattributes
as documented above and commit it.git add -A && git commit -m "Use LF."
- Normalize line endings and commit any changes:
git add --renormalize . && git commit -m "Convert CRLF to LF."
- If there were changes, add the CRLF to LF commit ID to a
.git-blame-ignore-revs
file at the root of your repository.git rev-parse HEAD >> .git-blame-ignore-revs && git add -A && git commit -m "Ignore CRLF to LF for git blame."
- Delete and restore your local files:
git rm --cached -r . && git reset --hard
- Tell
git blame
to ignore the commit IDs in the file:git config blame.ignoreRevsFile .git-blame-ignore-revs