Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save doevelopper/d54c533d3035072eb84f1bb62e41b553 to your computer and use it in GitHub Desktop.
Save doevelopper/d54c533d3035072eb84f1bb62e41b553 to your computer and use it in GitHub Desktop.

Recovering corrupted git objects

Only applicable when object is empty...

  1. Backup
  $> cp -a .git .git-old
  $> git fsck --full
  1. Remove empty files by using "rm", continue until none is left and the "missing blob" starts showing
  $> find .git/objects/ -size 0 -delete
  $> git reflog
  1. It will show "fatal: bad object HEAD"
  $> tail -n 2 .git/logs/refs/heads/<branch-i-was-working-on>
  1. Identify parent of last commit (the one HEAD is pointing to), easily recognizable as it will show up twice
  $> git show commit_parent
  $> git update-ref HEAD commit_parent
  $> git fsck --full
  1. There are some blobs left from outdated index, nuke and carry on
  $> rm .git/index
  $> git reset
  1. There should be only references to "dangling blobs", these are not errors, continue
  $> git status
  $> git add .
  $> git commit -m "Recovering from lost objects"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment