To undo the git reset command, you can use git reflog to revert back to the state before the reset.
For example, if your previous Git command was something like git reset –hard HEAD~1, you can undo the command by running:
$ git reset HEAD@{1}
The above command might not be the right one if you already did some other operations after the hazardous command. But no worries! Make sure to read the detailed answer as it teaches you how the above command works and how to get the lost changes back in any situation.
This guide teaches you how to undo git reset commands with git reflog. More importantly, you learn how git reference log keeps track of all the Git operations automatically and how you can reset your project to time before running operations.
Undo ‘git reset’ with ‘git reflog’
Git automatically keeps track of all the Git commands you do. These are stored in what’s called a reference log. In the Git reference log, you find past actions like checkouts, commits, resets, merges, and such. In a sense, the reference log stores entire project history.
To view the Git reference log of your project, run git reflog.
$ git reflog
The reference log works very similarly to your commit log. Each reference log action has a unique ID via which you can access it. In this guide, we’ll refer to these reference log actions as commits.
For instance, here’s my example project’s reference log:

On the left-hand side, the yellow number-character combos are the commit IDs of the reference log commits. The HEAD@{N} notation offers another way to refer to these actions.
If you ran a hazardous git command, such as a hard reset, and want to restore the lost changes:
- Open up the git reflog.
- View the reflog commit of the bad command (usually the most recent one).
- Run git reset on the commit either by using the commit ID or the HEAD@{N} as a reference.
Example
Here’s an example project that’s under Git version control.

And here’s the commit history of the project:

Now, let’s do a hard reset all the way to the initial commit and push the changes to the remote:
$ git reset --hard HEAD~8 HEAD is now at 56d211b Initial commit
Let’s also force-push these changes to the remote:
$ git push --force-with-lease Total 0 (delta 0), reused 0 (delta 0) To https://github.com/artturijalli/exampleProject.git + 21c6c50...56d211b main -> main (forced update)
Now all the files and changes are gone both locally and remotely.


To your surprise, there’s still a way to get the files back and reset the project back to the state it was in before the hazardous command.
Let’s open up the reference log:
$ git reflog
This opens up the history of Git operations.
Take a look at the very first operation in the log:

This reflog commit represents the hard reset command you just accidentally executed.
To undo the bad command, reset to one operation before, that is, to HEAD@{1}.
$ git reset --hard HEAD@{1}
Now your project is back to the state where it was before you ran the git reset –hard command to the initial commit. You can verify this by using git log:

And to update the remote repository back to the state it was before, you can now forcefully push these changes to the remote:
$ git push --force-with-lease
Summary
Today you learned how to undo a git reset command.
To take home, Git keeps track of all the operations you do automatically. These include checkouts, resets, commits, and such.
If you run a hazardous commit and want to undo it, you can search for the operation in the git reflog. Once you find the reflog commit of your bad command, you can reset to the previous commit to get back to the state where the project was before.
Thanks for reading. Happy coding!
Read Also
About the Author
- I'm an entrepreneur and a blogger from Finland. My goal is to make coding and tech easier for you with comprehensive guides and reviews.
Recent Posts
Artificial Intelligence2023.05.16Humata AI Review (2023): Best PDF Analyzer (to Understand Files)
Python2023.05.139 Tips to Use ChatGPT to Write Code (in 2023)
Artificial Intelligence2023.04.1114 Best AI Website Builders of 2023 (Free & Paid Solutions)
JavaScript2023.02.16How to Pass a Variable from HTML Page to Another (w/ JavaScript)