How can I fix a reverted git commit?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When you revert a Git commit with git revert, Git creates a new commit that undoes the changes. If you later want those changes back, you cannot simply re-merge or cherry-pick the original commit — Git sees the revert as intentional and skips the already-applied changes. The fix is to "revert the revert" — create a new commit that undoes the revert commit, effectively re-applying the original changes. Use git revert <revert-commit-hash> to undo the revert, or git cherry-pick the original commit if the history is complex.
Understanding Git Revert
git revert does not delete or modify history. It creates a new commit with the inverse diff. The original commit (abc1234) still exists in history, but its changes are undone by the revert commit (999aaaa).
Fix 1: Revert the Revert
This is the cleanest approach for shared branches. It preserves full history and creates an explicit record of the revert-then-re-apply decision.
Fix 2: Cherry-Pick the Original Commit
Cherry-picking copies the original commit's changes into a new commit. This works when you want to selectively re-apply changes, but on the same branch where the revert exists, Git may detect that the changes were already applied and reverted.
Fix 3: Reverted Merge Commit
Reverting a merge commit is especially tricky. If you later try to re-merge the feature branch, Git thinks all its changes are already in the target branch (because the merge commit exists in history). You must revert the revert first.
Fix 4: Interactive Rebase (Unpushed Only)
Interactive rebase rewrites history, so only use this for commits that have not been pushed to a shared remote. After pushing, other developers have the revert in their history.
Fix 5: Re-Apply with Modifications
--no-commit stages the revert of the revert without creating a commit, giving you a chance to modify the changes before committing. This is useful when the original code needed fixes.
Identifying the Revert Commit
Common Pitfalls
- Trying to re-merge a reverted merge branch: After reverting a merge, Git considers all the merged changes as already applied. Re-merging the same branch is a no-op. You must first revert the revert commit, then merge new changes from the feature branch.
- Using
git reseton pushed commits:git reset --hardremoves commits from local history but breaks the shared repository if the commits were already pushed. Other developers will have divergent histories. Usegit revertfor pushed commits. - Forgetting
--no-commitwhen modifications are needed:git revert <hash>creates a commit immediately. If you need to modify the re-applied changes (fix the bug that caused the revert), use--no-committo stage changes first. - Cherry-picking a commit that was already reverted on the same branch: Cherry-picking the original commit onto a branch where its revert exists can cause conflicts or be silently ignored because Git sees the net effect as zero. Revert the revert instead.
- Not communicating the revert-revert to the team: Reverting a revert creates confusing history (
Revert "Revert "Feature X""). Write a clear commit message explaining why the changes are being re-applied and what was fixed, so other developers understand the intent.
Summary
- To re-apply a reverted commit, "revert the revert" with
git revert <revert-hash> - For reverted merge commits, revert the revert before attempting to re-merge the feature branch
- Use
--no-committo stage the re-applied changes and make modifications before committing - Only use
git rebase -ito drop revert commits that have NOT been pushed to shared remotes - Use
git log --grep="Revert"to find revert commits in history - Write clear commit messages explaining the revert-revert to avoid confusing team members
Related reading
- How can I fix the Git error object file ... is empty?
- How can I fully delete a Git repository created with init?
- How can I fully delete a Git repository created with init?
- How can I generate a Git patch for a specific commit?
- How can I get a list of Git branches, ordered by most recent commit?
- How can I get my C code to automatically print out its Git version hash?
- How can I get the assembly file version
- How can I get the diff between all the commits that occurred between two dates with Git?
.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.