Git: How to restore a file history blame track after changed its EOL?

475 Views Asked by At

we have some files originally created under Windows with EOL (CRLF) and others with Linux (LF), sometimes the IDE (or a bad configured Git after a fresh reinstall) changed those EOL overriding and making us lose the entire history of the files (blame is completely useless, just with -w works).

I tried tracking the last time the file was ok, before the switch of EoL and replacing it but didn't work, retrieving that file from another branch. Since the file always had minimal changes I didn't notice this problem after several commits and merges, I have a 6 months old branch with the original EOL.

How can I fix these files?

2

There are 2 best solutions below

4
On

You need to run blame with -w:

git blame -w some-file

There are other options to control this, and -w means "do not consider spaces/tabs/new line changes".

Just in case, you should not have these EOL changes in the first place. They are a pain to deal with long term.

Self-promotion warning: this "article" (for lack of a better word) talks about EOL changes when merging. It also points to a recipe to fix EOL problems... but with a few considerations to use the script.... you might develop something from that (no tracking, no monetization). http://www.ezconflict.com/en/conflictsse16.html#x80-1200003.2

2
On

Blame uses your text conversions, so you can run any prepass you want on the content.

echo itssamsfault.c diff=demanglenewlines >>.git/info/attributes
git -c diff.demanglenewlines.textconv="sed 's,\r+$,,'" blame itssamsfault.c