how to restore corrupeted git file

C++
8

Step 1: Make a backup of .git (in fact I do this in between every step that changes something, but with a new copy-to name, e.g. .git-old-1, .git-old-2, etc.):

cp -a .git .git-old
Step 2: Run

git fsck --full
you will get this error message

For example: error: object file .git/objects/0A/dsdadadadaaeer4r3434343434334f is empty

Step 3: Remove the above empty file this is in your .git/objects/ folder. Continue deleting the empty files.

Step 4: After deleting all of the empty files, now run

git fsck --full
Step 5: Try git reflog. Fail because HEAD is broken.

Step 6: Manually get the reflog:

git log origin/master..HEAD
Step 7: Note that from Step 6 we learned that the HEAD is currently pointing to the very last commit. So let's try to just look at the parent commit:

git reset HEAD@{5} // resetting hard to refs with the changes. If it works here you can stop here instead of going to step 8

git show commit-id 
Step 8: So now we need to point HEAD to commit-id

git update-ref HEAD commit-id
Hope this will work for you.


Source

Also in C++: