3

I have a PNG icon in my repository which got corrupt after pushing it to the remote. Using git check-attr I found out that git treats it as text file and thus converted its line endings to LF according to the repository's .gitattributes.

Is there a way to recover the original file?

danijar
  • 32,406
  • 45
  • 166
  • 297
  • I guess git treated it as text file from the very beginning? If so then I think it would be very difficult to save the file. Git converts all windows style line endings (`\r\n`) to unix style ones (`\n`) and now you have no way of knowing which occurences of the `\n` byte used to be `\r\n` ones. – Sascha Wolf Jun 17 '14 at 14:53
  • @Zeeker That's what I feared. Is there some kind of cache inside `.git` where the originals are kept? – danijar Jun 17 '14 at 15:08
  • As far as I know: No. But I'll take a look if I find anything. – Sascha Wolf Jun 17 '14 at 15:10
  • I played a little bit around with git and it really seems like there is no way to recover the original state of the file. I'm sorry. – Sascha Wolf Jun 17 '14 at 15:26
  • @Zeeker Thanks for your effort. You could write an answer stating that it's not possible and I'll mark it as accepted. – danijar Jun 17 '14 at 15:46
  • 1
    If the icon file is small there should be few \r\n newlines in the original, it could be feasible to try, by brute force, to do the reverse change trying all the combinations till it produces a valid PNG file. – leonbloy Jun 18 '14 at 00:20

1 Answers1

4

Sadly it seems like that it's not possible to retrieve the original version of an "autocrlfed" file, as long as there is no untouched version of the file in the history (means no autocrlf used).

With core.autocrlf true git replaces all occurences of the windows line ending (\r\n) with the unix style line ending (\n). After that it's not possible to determine which \n bytes used to be encoded as \r\n bytes.

Therefore I'm obliged to conclude that it's impossible to restore the file.

This is a reason to avoid autocrlf and to handle line endings yourself. In general I recommend to avoid autocrlf; more information on this topic can be found here.

Community
  • 1
  • 1
Sascha Wolf
  • 18,810
  • 4
  • 51
  • 73