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.
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:
git reset HEAD~1
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
Copy this hash, and then run
git revert 62ff517cc7c358eaf0bffdebbbe1b38dea92ba0f
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.
git reset --hard HEAD^
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.
- › Windows 11 Will Show Account Notifications in the Start Menu
- › Google Docs Will Help You Organize Long Documents
- › Chef ChatGPT: Cooking a Recipe Made by AI
- › 6 Things You Need for the Perfect Home Racing Sim System
- › PWM Fan vs. DC Fan: Which Is Better for PC Cooling?
- › The Best Portable Printers of 2023