Git revert Command
In this page:
What is Git Revert?
git revert creates a brand-new commit whose changes exactly cancel out an earlier commit, rather than deleting that commit from history. This makes it the safe choice for undoing something on a branch other people have already pulled, since nobody's history gets rewritten.
Example: What is Git Revert?
git revert a1b2c3d
Reverting Multiple Commits
Reverting a range of commits (e.g. git revert A..B) doesn't squash them into one undo — Git creates a separate revert commit for each original commit in the range, applied in reverse order. This keeps the history readable, since you can trace exactly which original commit each revert corresponds to.
Example: Reverting Multiple Commits
git revert a1b2c3d..e4f5g6h
Reverting without Committing
Passing --no-commit performs the revert's file changes and stages them without immediately creating the revert commit, giving you a chance to inspect the diff, tweak it, or bundle several reverts into a single commit before finalizing.
Example: Reverting without Committing
git revert --no-commit a1b2c3d
Reverting a Merge Commit
Reverting a merge commit is ambiguous by default, because a merge has two parent lines and Git needs to know which one is the "mainline" to revert against — you specify it with -m 1 (usually the branch you merged into). Getting the parent number wrong silently reverts the wrong side of history.
Example: Reverting a Merge Commit
git revert -m 1 a1b2c3d
Resolving Revert Conflicts
If the code a revert is trying to undo has since been changed by later commits, Git can't cleanly apply the reverse patch and raises a conflict just like a merge would. You resolve it the same way — edit the conflicted files, stage them, and run git revert --continue.
Example: Resolving Revert Conflicts
git revert a1b2c3d
# fix conflicts in file.txt
git add file.txt
git revert --continue
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: