Skip to content

Instantly share code, notes, and snippets.

View ArtemPisarenko's full-sized avatar

Artem Pisarenko ArtemPisarenko

View GitHub Profile
@bhcleek
bhcleek / recover-index.md
Last active November 6, 2022 11:09
recover lost index

to recover files that were added to the index but whose changes were lost (e.g. git reset --hard)

git fsck --unreachable | grep commit | cut -d\  -f3 | xargs git show
  1. git fsck --unreachable to get all the items that are unreachable
  2. grep commit to filter out all entries except for commits (the index will show up as a commit)
  3. cut -d\ -f3 to filter out all but the SHA1s
  4. xargs git show to show all of the contents of the objects.

Once you've identified the SHA1 that contains the changes that were lost, check it out to get the working tree back into the state of the index at the time git reset --hard was run.