How to recover a git repository that has been corrupted by having line endings changes from CRLF to LF?

StackOverflow https://stackoverflow.com/questions/16847406

  •  30-05-2022
  •  | 
  •  

문제

I changed all of the line endings in my project from dos mode to unix mode by removing all of the carriage returns from any file that had them:

grep -lIUr '^M' . | xargs sed -i 's/^M//'

I thought that it would ignore hidden dotfiles like .git/, but it didn't, and now my git repository has been mangled.

git fsck

gives

Checking object directories: 100% (256/256), done.
error: inflate: data stream error (incorrect data check)
fatal: loose object 62f7323e2a025640a25214f13a670097876c1683 (stored in .git/objects/62/f7323e2a025640a25214f13a670097876c1683) is corrupt

There aren't any other copies of the repository checked out anywhere. Is there any way to recover from this? Is my repo dead?

도움이 되었습니까?

해결책

Yes, it is. No luck, since all ^M are gone: maybe you can partially recover history and build another repo, but nothing more.

For the future, use greater care and proper tools like dos2unix for converting line endings.

다른 팁

In the future (does not help you now), you can limit your command to files in the repository.

grep -lIUr '^M' `git ls-files` | xargs sed -i 's/^M//'
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top