Normalise line endings in a git repository
I was modifying a file and noticed that git showed the entire file had changed when I did a diff, even though I had only edited one line. This can happen when we have inconsistent line endings, some files were using CRLF
while others had LF
. This made it look like every line was modified meaning the actual changes were ‘lost’ in the diff.
You can determine line endings per file with this command.
git ls-files --eol
The output looks something like this where we can see both lf and crlf exist.
i/lf w/lf attr/ path/to/file/one.ts
i/crlf w/crlf attr/ path/to/file/two.ts
In order to fix this, I found that git provides a way to ‘renormalise’ line endings using .gitattributes
. By setting * text=auto
, git ensures that files are stored with LF
in the repository while allowing Windows users to check them out with CRLF
if needed.
The following is copied here from the docs.
From a clean working directory:
$ echo "* text=auto" >.gitattributes
$ git add --renormalize .
$ git status # Show files that will be normalized
$ git commit -m "Introduce end-of-line normalization"
Update files
At this point local files in my IDE were still showing CRLF
. In order to update them I found this information which is as follows…
…the files in your working copy still retain their old line endings. If you want to update them, make sure your working tree is clean and use:
git rm --cached -r .
git reset --hard
This updated my local working copy and I could now see all line endings were LF
.
How renormalisation affects history
- files are updated in the working tree to match the
.gitattributes
settings - git stages the changes but does not modify past commits
- when you commit, only that commit will have your name as the author
- previous commits remain unchanged, keeping their original authors
Summary
After researching how to do this and loosely documenting it I found this comprehensive article about this very subject, go and read that.