DEV Community


How to recover our commit after a git reset --hard ?

danyson profile image Danyson Updated on ・1 min read

While doing a "git reset" with a "--hard" flag in any branch it will reset the index and working tree which means not only our index is discarded but also along with contents in our working file. Simply, any changes to tracked files in the working tree since are discarded.

Now, how do we get back to our last commit. A simple way is there.

So, whenever git does something extreme like changing or rewinding branches, it records that in "reflog" a reference log.
Now, check the output of "git reflog", it will tell you all transitions our branch ever had.
We can then use git show to check and git checkout to get back.

Flow of recovery :

git reflog

<commit_id> (HEAD -> main, origin/main, origin/HEAD) HEAD@{0}: commit: <message>
<commit_id> HEAD@{1}: commit: <message>
<commit_id> HEAD@{2}: commit: <message>
Enter fullscreen mode Exit fullscreen mode

git show <commit_id>

git checkout <commit_id>

Visit My Personal Blog @

Discussion (0)

Forem Open with the Forem app