If you have reverted a commit in a Git repository, there are a few things you can do to help ensure that your work is preserved and that the commits are properly tracked. First, make sure that you have git status -l . This will show you the current state of your repository and its commits. If there are any changes that need to be made to your repository before it can be used for development, then it’s important to back up your work first. Next, create a new branch for your project. This will allow you to work on different parts of your project while still keeping track of the overall progress of the project. Finally, make sure that all of your changes are committed into the new branch and pushed into the main Git repository.


If you’ve just reverted a Git commit, and accidentally deleted a file or piece of code because of it, don’t worry. Git keeps track of everything, and recovering the reverted file is easy to do both from the command line and most GUI Git clients.

Don’t Panic

Luckily, Git keeps track of everything, even reverted commits. You haven’t deleted anything yet.

You may have run into this while trying to “delete” a Git commit. However, you can’t really delete commits once you make them. You can only reset to a previous commit and ignore the local changes, or revert the changes.

For example, in my case with this problem, I had just initialized my Git repository, and accidentally staged everything without writing my .gitignore file, which committed ./bin/ folders and other junk I didn’t want. So, without thinking about it, I clicked “revert” in my Git client, and watched in horror as my entire directory got deleted. Oops.

The problem is that “reverting” a commit is not the same as unstaging the changes, and can have unexpected effects on your local files that can only be fixed through Git. This looks like a serious problem until you realize that all the data is still stored in the .git/ folder of your repository, so it can be recovered.

It doesn’t help that it’s weirdly named, and the actual way to remove an unwanted commit is to do a “soft reset” back to the last commit behind the HEAD. So, next time, if you’d like to undo a commit (to edit your changes and re-commit) use the following command:

For clarification—this is not a “hard” reset, which removes all your local changes as well.

Fixing Reverted Commits

The fix is pretty simple. Whenever you do a “git revert,” Git makes a new commit with opposite changes to the commit being reverted. If you created a file, that file is removed, and the commit reflects that.

The fix is to apply that reverting commit, and then revert it back, which will un-revert the changes. This will work even if the revert commit is not at the HEAD of your repository; otherwise, doing a hard reset would also work and would free your repo from embarrassing “revert” and “unrevert” commits that your coworkers will laugh at.

To find the hash for the commit, you can run git log:

Copy this hash, and then run git revert:

This should fix the repository, though you may have to commit the revert manually if the automatic commit failed.

In most GUI Git clients, the process is extremely simple. Just click “revert” on the commit.

Reverting Without a Trace

If you’re at the HEAD of your commit tree, and you’d like to do this without making new commits, and you haven’t pushed the change yet, you can hard reset your local branch to the old commit.

The reason this needs to be a hard reset is because a soft reset would still include the unstaged changes from the revert. You want to reset without the local changes.

You could also soft reset and discard the local changes manually.