How to get your changes back after an accidental hard reset in git?

Written by Sahil Bhatacharya in Accidents Recovery

If you made git reset --hard by mistake, possibilities are you can still get your commit back, as git holds a log of everything for a few days. This is only valid if your work is backed up, i.e., by committed or stashed. As git reset --hard removes uncommitted changes, so be careful before going into trouble.

The much reliable choice is to run the following command.

git reset --keep

Alternately, you can check the list of your past commits and choose the SHA of the commit you want:

git reflog

Now, run the following command to get your changes back:

git reset --hard SHA1234

Sign up below to receive updates, tips and more handy tutorials when git screws up. It’s good stuff & I don’t spam.