🎓 A1Learner
🔍
0 of 50 lessons completed

Revert a Commit

Revert undoes a snapshot by making a new one. The old snapshot stays in the history, and the new one takes its change back.

Undo the tea snapshot

One line is removed: the tea line the snapshot had added.

$ git revert --no-edit HEAD

Both snapshots sit in the log

The tea snapshot stays. The new snapshot above it takes the change back.

$ git log --oneline

Example

$ git revert --no-edit HEAD
$ git log --oneline

Key points

  • Revert with a hash undoes that snapshot.
  • Git makes a new snapshot that reverses the change.
  • The old snapshot stays in the log. Nothing is rewritten.
  • Revert is safe for shared work, because history is only added to.
  • The --no-edit form takes the message Git offers.
  • Use revert when others may already have the snapshot.

Here, the tea snapshot stays in the log. The new snapshot above it takes the tea line back.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Why is revert safe for shared work?