Git Notes
Learn git revert to safely undo committed changes while maintaining history, understanding revert vs reset, and reverting specific commits in collaborative environments.
Imagine this scenario: your team deployed code on Friday afternoon, and by Saturday morning users are reporting a critical bug. The problematic commit has already been pushed, pulled by teammates, and deployed to production. You cannot simply delete that commit from history because your entire team's work is built on top of it. This is exactly where git revert saves the day.
git revert creates a new commit that perfectly undoes the changes introduced by a previous commit. It does not erase history or rewrite anything. Instead, it says "I acknowledge what happened, and here is a new commit that reverses it." Your Git log tells the complete story: what was done, and what was undone.
Basic Revert Operation
# Revert a specific commit by its hash
git revert abc1234
# Git opens your editor with a default message like:
# Revert "feat: add new payment gateway"
# This reverts commit abc1234...
# Save and close - a new commit is createdAfter running this, your log shows both the original commit and the revert commit:
git log --oneline
# f5e6d7c Revert "feat: add new payment gateway"
# abc1234 feat: add new payment gateway
# 9b8a7c6 previous commit...This is the beauty of revert. Anyone looking at the history immediately understands what happened and why.
Reverting the Most Recent Commit
The most common case is undoing whatever was just committed:
# Revert the last commit (HEAD)
git revert HEAD
# Revert the second-to-last commit
git revert HEAD~1
# Revert the third-to-last commit
git revert HEAD~2Revert Without Committing
Sometimes you want to stage the revert changes but not commit them immediately. This is useful when you want to revert multiple commits and combine the undos into a single commit:
# Revert but do not auto-commit
git revert --no-commit abc1234
git revert --no-commit def5678
# Now both reverts are staged but not committed
git status # Shows all the reversed changes staged
# Commit them together
git commit -m "revert: undo payment gateway and email changes"The --no-commit flag (or -n for short) applies the reversal to your working directory and staging area without creating a commit, giving you full control over the final result.
Reverting Multiple Commits
When you need to undo a range of commits:
# Revert a range (from older to newer, exclusive of start)
git revert abc1234..def5678
# This creates one revert commit per original commit in the range
# Each gets its own "Revert" commit message
# Or revert multiple specific commits individually
git revert abc1234
git revert ghi9012
git revert mno3456For a cleaner history when reverting multiple commits:
# Combine multiple reverts into one commit
git revert --no-commit abc1234..def5678
git commit -m "revert: undo feature-X (commits abc..def)"Handling Conflicts During Revert
Reverting is not always clean. If code has changed since the original commit, conflicts can arise:
git revert abc1234
# CONFLICT (content): Merge conflict in src/payment.js
# error: could not revert abc1234
# Step 1: Open the file and resolve conflicts
# The conflict markers show what revert wants vs current state
code src/payment.js
# Step 2: After resolving, stage the file
git add src/payment.js
# Step 3: Continue the revert
git revert --continue
# Or if you want to abandon the revert entirely
git revert --abortRevert vs Reset: The Critical Difference
This is one of the most important distinctions in Git and a frequent interview question:
The simple rule: if the commit exists only on your local machine, you can use either. If it has been pushed to a shared branch, always use git revert.
Reverting a Merge Commit
Merge commits are special because they have two parents. You must tell Git which parent to revert toward:
# Reverting a merge commit requires specifying the parent
git revert -m 1 abc1234
# -m 1 means "keep the first parent's changes" (usually main)
# -m 2 means "keep the second parent's changes" (the merged branch)This is a tricky operation. When you revert a merge, the changes from the merged branch are undone, but Git remembers the merge happened. If you later want to re-merge that branch, you need to revert the revert first:
# Original merge
git merge feature/payments # merge commit: abc1234
# Revert the merge
git revert -m 1 abc1234 # revert commit: def5678
# Later, to re-introduce the feature, revert the revert
git revert def5678 # un-reverts the original mergeReal-World Emergency Workflow
Here is how a production hotfix typically works using revert:
# 1. Identify the problematic commit
git log --oneline
# abc1234 feat: new checkout flow <- THIS broke things
# 2. Revert it immediately
git revert abc1234
# Creates: "Revert 'feat: new checkout flow'"
# 3. Push the fix
git push origin main
# 4. CI/CD deploys the reverted code
# Production is fixed within minutes
# 5. Later, fix the original code on a branch
git checkout -b fix/checkout-flow
# ... make fixes ...
git revert HEAD~1 # Revert the revert to bring code back
# ... or rewrite the feature properlyCommon Mistakes
Mistake 1: Using git reset on shared branches instead of git revert. This rewrites history that others depend on and causes major synchronization problems across the team.
Mistake 2: Forgetting the -m flag when reverting merge commits. Git will tell you it cannot determine which parent to revert against.
Mistake 3: Trying to re-merge a branch after reverting its merge. You must revert the revert commit first, otherwise Git thinks those changes are already incorporated.
Interview Questions
Q1: When should you use git revert instead of git reset?
Use revert whenever commits have been pushed to a shared branch. Revert creates a new commit preserving history, so teammates are not affected. Reset rewrites history and requires force push, which breaks everyone else's local repository. Only use reset for local unpushed commits.
Q2: How does git revert handle merge commits?
Merge commits have two parents, so you must specify which parent side to keep with the -m flag. -m 1 keeps the mainline parent (undoes the merged branch changes). This is the most common case. Be aware that re-merging the same branch later requires reverting the revert first.
Q3: What happens when a revert creates conflicts?
Conflicts arise when the codebase has changed since the original commit. You resolve them the same way as merge conflicts: edit the files, remove conflict markers, stage with git add, then run git revert --continue. You can abort with git revert --abort if needed.
Q4: Can you revert a revert?
Yes, reverting a revert re-introduces the original changes. This is actually the recommended way to "un-revert" code. It creates a clean history showing that changes were removed, then later re-introduced, with full traceability.
Q5: How does your team handle emergency production rollbacks?
We identify the problematic commit, revert it on main, push immediately to trigger deployment of the fix. The revert is fast and safe. Then we create a branch to properly fix the issue, revert the revert, apply the fix, and merge through normal PR process.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Revert - Safely Undo Changes.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, undoing, changes, revert, git revert - safely undo changes
Related Git & GitHub Topics