Undo a Hard Reset with the git reflog Command

Learn to recover the changes and commits in case of a hard reset.

The git reflog command

We can undo a hard reset with the git reflog command.

After we do a hard reset, we can’t see the changes of that commit in the git log or working directory anymore. But still, we can use reflog to rescue recently deleted commits.

We’ve run these commands in the terminal below to see the effect of git reset --hard command:

 $ mkdir ReflogExample
 $ cd ReflogExample
 $ git init
 $ touch file1.txt
 $ git add file1.txt
 $ git commit -m "adding file1.txt"
 $ touch file2.txt
 $ git add file2.txt
 $ git commit -m "adding file2.txt"
 $ git status
 $ git reflog
 $ git reset --hard HEAD~1
 $ git status
 $ git reflog

Get hands-on with 1200+ tech skills courses.