← Back to Git Course | Chapter 5: Undoing Changes | Lesson 2 of 6

Git revert Command

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?

bash
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

bash
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

bash
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

bash
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

bash
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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.