Git cancel a revert
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A git revert creates a new commit that undoes the changes from a previous commit, which is the safe way to undo work on shared branches. But sometimes the revert itself was a mistake, and you need to undo it. Depending on whether the revert has been pushed to a remote, you have several strategies available, each with different tradeoffs around history cleanliness and team impact.
How git revert Works
Before canceling a revert, it helps to understand what happened. git revert does not delete a commit. It creates a new commit whose changes are the exact inverse of the target commit.
Commit D' contains the opposite of every change in D. The original commit D remains in history. This is why reverting is safe for shared branches, but it also means canceling the revert requires adding yet another commit or rewriting history.
Method 1 - Revert the Revert Commit
The safest approach, and the only one suitable when the revert has already been pushed to a shared branch, is to revert the revert commit itself.
After this, the history looks like this.
Commit D'' reapplies the original changes from D by inverting D'. The net effect is that D's changes are restored. This approach is completely safe for shared repositories because it only adds new commits.
Method 2 - git reset (Before Pushing)
If the revert commit has not been pushed yet, you can remove it entirely with git reset. This keeps the history clean by erasing the revert commit as if it never happened.
After a hard reset, the history returns to its state before the revert.
Use --soft when you want to inspect the staged changes before deciding what to do. Use --hard only when you are certain you want to discard the revert completely.
Warning - Never use git reset to remove commits that have already been pushed to a shared branch. Other team members who have pulled those commits will encounter conflicts.
Method 3 - Interactive Rebase to Drop the Revert
If the revert commit sits among several local commits that have not been pushed, an interactive rebase lets you selectively remove it while keeping other commits.
This opens an editor showing recent commits.
Change pick to drop (or delete the line) for the revert commit.
Save and close the editor. Git replays the remaining commits without the revert.
Note that commit E' gets a new hash because its parent changed. This is a history rewrite, so only use this method on commits that have not been pushed.
Recovering from a Pushed Revert
When a revert has been pushed and other developers have pulled it, your only safe option is Method 1 (revert the revert). Attempting to reset or rebase a shared branch causes divergent histories and forces everyone to reconcile their local copies.
Here is the complete workflow for recovering from a pushed revert.
If the original commit that was reverted contained conflicts with newer code, the revert-of-revert may produce merge conflicts. Resolve them as you would any merge conflict.
Using git reflog as a Safety Net
If you accidentally ran git reset --hard and lost the revert commit that you actually wanted to keep, git reflog records every HEAD movement and lets you recover.
The reflog retains entries for at least 30 days by default, so you have a generous window to recover from mistakes.
Choosing the Right Method
The decision depends on a single question - has the revert been pushed to a shared branch?
When in doubt, reverting the revert is always safe. It adds a commit to history, but it never disrupts other developers.
Common Pitfalls
- Using git reset on pushed commits - Resetting commits that others have pulled creates divergent histories, forcing teammates to resolve confusing conflicts or re-clone the repository.
- Confusing revert with reset -
git revertcreates a new commit that undoes changes.git resetmoves the branch pointer backward. Using the wrong command leads to unexpected results. - Forgetting to push after reverting the revert - The fix only exists locally until you push. Team members continue working with the reverted code until the revert-of-revert reaches the remote.
- Ignoring merge conflicts during revert-of-revert - If code has changed since the original commit, the revert-of-revert may conflict. Skipping careful conflict resolution can introduce bugs.
- Not communicating with the team - Reverting and re-reverting on shared branches without notifying collaborators causes confusion. Always inform the team when manipulating shared history.
Summary
- Revert the revert commit with
git revert <hash>when the revert has already been pushed to a shared branch. This is the safest approach. - Use
git reset --hard HEAD~1to erase an unpushed revert commit cleanly, as if it never happened. - Use interactive rebase to drop a revert commit when it sits among other unpushed local commits you want to keep.
- The
git reflogcommand is your safety net for recovering commits lost during a reset. - Always prefer adding new commits over rewriting history on shared branches to avoid disrupting collaborators.
- Communicate with your team whenever you revert or undo a revert on a shared branch.
Related reading
- git cannot apply binary patch without full index line
- Git, cannot checkout branch - error, pathspec ''...'' did not match any file(s) known to git
- Git cannot checkout branch - error pathspec '...' did not match any files known to git
- Git Cannot see new remote branch
- Git can't undo local changes error path ... is unmerged
- Git checkout updating paths is incompatible with switching branches
- Git Checkout warning unable to unlink files, permission denied
- git cherry-pick says ...38c74d is a merge but no -m option was given
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.