Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to normalize working tree line endings in Git?

I have cloned a repository that had inconsistend line endings. I have added a .gitattributes that sets the text attribute for the files I want to normalize. Now when I commit changes I get the message:

warning: CRLF will be replaced by LF in FILE. The file will have its original line endings in your working directory. 

How can I make git normalize my working copy of the file for me? Preferably I would like git to normalize the entire working tree.

like image 558
user11171 Avatar asked Mar 26 '13 15:03

user11171


People also ask

What is normalize line endings?

Normalizing the line endings is just making sure that all of the line ending characters are consistent. It prevents one line from ending in \r\n and another ending with \r or \n ; the first is the Windows line end pair, while the others are typically used for Mac or Linux files.

Which is better CRLF or LF?

For this reason, core. autocrlf=true tends to be recommended setting for Windows developers since it guarantees LF in the remote copy of your code while allowing you to use CRLF in your working tree for full compatibility with Windows editors and file formats.

How does Git store line endings?

Git store whole files as "blobs" and only do conversion when reading (checking out) or writing (indexing). Git does not store "neutralized lines". So yes, Git can save files with mixed line endings, but that's usually a bad practice to avoid. So, this means, checkout a file with Line Ending A (= .


2 Answers

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

git add --renormalize .  # Update index with renormalized files git status               # Show the files that will be normalized git 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 to git reset         # re-scan the working directory git status        # Show files that will be normalized git add -u git add .gitattributes git 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 directory git add . git status        # Show files that will be normalized git 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.

like image 197
John Szakmeister Avatar answered Sep 19 '22 14:09

John Szakmeister


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:

  • https://git-scm.com/docs/gitattributes#_end_of_line_conversion
  • https://help.github.com/en/github/using-git/configuring-git-to-handle-line-endings#refreshing-a-repository-after-changing-line-endings
like image 33
philippn Avatar answered Sep 18 '22 14:09

philippn