Instead, the recommended approach would be to use the revert command. At this point it's highly advised that you do not use something like git reset since you'd be rewriting history.
So let's say you committed your code and then pushed it to the remote repository. So if you have uncommitted changes that you want to keep, then this is likely the option you want. This option works much the same way as git reset -hard, but it only affects the commit history, not your working directory or staging index. The other option you may consider is the -soft option. The stash command saves your working changes (without any commits or changes to the tree), and then stash pop brings them back. To avoid losing any working changes, you can use the stash and stash pop commands: $ git stash $ git reset -hard $ git stash pop This means that by using just this command you'll not only revert to a previous commit, but you'll lose all working changes in the process. This includes the commit history reference pointers, the staging index, and your working directory. Using the -hard option, everything is reverted back to the specified commit. The reset command has three different options, two of which we'll describe here: $ git reset -hard The only way to find and recover these unreferenced commits is with git reflog. While this is an effective solution, it's a dangerous one since you're rewriting history and leaving the "deleted" commits unreferenced, or "orphaned". If you haven't yet published your commits to a remote repository, like GitHub, then you can essentially delete previous commits by using the reset command. This is a complicated topic (which is true for many Git topics in general), so make sure you follow the instructions that best suits your needs. In this article I'll show a few ways to revert your commits, depending on your use-case. Whether you accidentally commit changes, or just realized your previous committed code isn't what you wanted, often times you'll need to revert a previous commit in Git. This equally applies to version control tools as well. If I've learned anything in my 15+ years of programming, it's that mistakes are common, and I make a lot of them.