How to Cancel or Undo a git revert in Git

In Git, the git revert command is commonly used to safely undo changes by creating a new commit that reverses the effect of a previous one. Unlike git reset, revert doesn’t alter commit history, making it ideal for collaborative workflows.

But what if you ran git revert by mistake or want to cancel the changes it introduced? Can you undo a revert?

Yes, you can! In this guide, we’ll explore different scenarios and show you how to cancel or undo a git revert effectively.


🔄 What Does git revert Do?

When you run:

git revert <commit-hash>

Git creates a new commit that undoes the changes introduced by the specified commit. It does not delete the original commit—it just adds a new one to reverse it.


🚫 Scenario 1: You Reverted the Wrong Commit and Want to Undo It

If you’ve just run git revert and committed the change, you can use:

git revert <revert-commit-hash>

This reverts the revert—effectively restoring the original commit.

Example:

git log --oneline

Let’s say this is your history:

abc1234 Revert "Add new feature"
789abcd Add new feature

To undo the revert:

git revert abc1234

You’ll now have a new commit that restores "Add new feature".

✅ Tip: Use git log to identify the hash of the revert commit.


✋ Scenario 2: You Reverted But Haven’t Committed Yet

If you’ve started a revert but haven’t committed (e.g., you ran git revert and Git opened your text editor for a commit message), and you want to cancel the revert:

git revert --abort

This cancels the operation and leaves your working directory as it was before.

🔁 Only works while a revert is in progress.


🧹 Scenario 3: Revert Has Been Committed, But You Want to Remove It from History (Not Recommended for Shared Repos)

If you’re working in a private branch or haven’t pushed yet, you can use:

git reset --hard HEAD~1

This will remove the latest commit (including the revert). Use with caution—this rewrites history.

⚠️ Don’t use this if you’ve already pushed to a shared repository, unless you coordinate with your team.


🔍 Summary

GoalSolution
Revert the revertgit revert <revert-commit-hash>
Cancel an in-progress revertgit revert --abort
Delete the revert commit (unsafe on shared branches)git reset --hard HEAD~1

✅ Final Thoughts

While git revert is a safe and powerful command, mistakes can happen. Thankfully, Git gives you the tools to recover—whether you need to cancel, undo, or redo a revert. Always double-check commit hashes and consider whether you’re working on a shared branch before modifying history.

Sharing Is Caring:

Leave a Comment