Lesson 22 +20 XP

Git Revert

Git Revert

git revert undoes a commit by creating a brand new commit that does the opposite of the old one.

The revert command

git revert abc123

Git looks at commit abc123, figures out what it changed, and creates a new commit that reverses those changes.

Why revert instead of delete?

Revert does not rewrite history. The old commit stays in the log, and a new "undo" commit appears on top. This is very safe:

  • Great for shared branches.
  • Other people can pull the fix without trouble.
  • You never lose the old history.

A simple example

git log --oneline
git revert abc123

The result is a new commit with a message like "Revert 'Broken change'".

Revert the latest commit

git revert HEAD

HEAD always points to your most recent commit.

TL;DR

  • git revert hash undoes a commit with a new commit.
  • History is not rewritten; it is added to.
  • The old commit remains in the log.
  • Safe to use on shared branches.