Restoring Lost Git Commits

Restoring Lost Git Commits

Have you faced any issue where you committed to local repository and then accidentally forgot to push the changes to remote and did a git reset --hard.

In simple steps you can restore your changes. When you actually do a git reset --hard git still has your commit as a dangling commit.Until you did git gc you have a good chance to recover your commit.

The below command shows a list of all dangling commits.

git fsck --lost-found 
Output : 
Checking object directories: 100% (256/256), done.
Checking objects: 100% (17697/17697), done.
dangling commit 8f1a8..
You can do a checkout and see your commit changes

 git checkout 8f1a8..
Now just do a git merge to get changes to your desired branch
git merge 8f1a8...
There you go!
You have successfully restored lost changes.

:)

Comments