Last Updated: August 21, 2017
· riyadhalnur
10309058 10152457564452360 4465802230248535815 n

Get back reset hard reset commits in git

If you're like me and accidentally hard reset the commits in a branch, there is still redemption after all.

Git keeps a history of all that goes on in a git directory for a few days before it empties it. If you're not too late, you can get back those commits with 2 commands only - reflog and rebase.

In your terminal, type in git reflog. This will list all the history for the git directory for the past few days. Note the hashes of the commits you want to get back.

Then, again in your terminal window, type git rebase <sha-1 commit hash> to rebase the reset commits on top of the current branch. Do this for all the commits that you want to undo resetting.

Note: you might need to do a hard push to origin if the remote branch was already pushed to before.

Say Thanks
Filed Under