How to normalize working tree line endings in Git? How to normalize working tree line endings in Git? git git

How to normalize working tree line endings in Git?


For those using v2.16 or better, you can simply use:

git add --renormalize .  # Update index with renormalized filesgit status               # Show the files that will be normalizedgit commit -m "Introduce end-of-line normalization"

These directions are straight out of the gitattributes. For older versions, the docs (prior to v2.12) provide a different answer:

rm .git/index     # Remove the index to force git togit reset         # re-scan the working directorygit status        # Show files that will be normalizedgit add -ugit add .gitattributesgit commit -m "Introduce end-of-line normalization"

Do this sequence after you have edited .gitattributes.

Update

It appears some users have had trouble with the above instructions. Updated docs for gitattributes (2.12 to 2.14) shows a new set of instructions (after editing the .gitattributes files):

git read-tree --empty   # Clean index, force re-scan of working directorygit add .git status        # Show files that will be normalizedgit commit -m "Introduce end-of-line normalization"

Thanks to @vossad01 for pointing this out.

Also, with either solution 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

Now the line endings will be correct in your working tree.


With Git client 2.16 and higher there is now a much simpler way to do this. Just use:

git add --renormalize .

Note: it's better to do this with a clean workspace. For details, see:


Alternative approach (differs only in command used)

Make sure you have no any pending changes in repository:

$ git status$ git stash

Modify .gitattributes so CRLF interpretation will changed:

$ echo "*.txt  text" >>.gitattributes$ git commit -m "Made .txt files a subject to CRLF normalization." -- .gitattributes

Remove data from index and refresh working directory:

$ git rm --cached -r .$ git reset --hard

Review CRLF fixes that Git proposes:

$ git ls-files --eol$ git status$ git diff

Agree with Git decision:

$ git add -u$ git commit -m "Normalized CRLF for .txt files"

Reload changes as if clean clone was done:

$ git rm --cached -r .$ git reset --hard