Git & GitHub Course
Git & GitHub
/
Intermediate

git revert

Definition

A safe 'undo' command. Instead of erasing a commit from history (like `git reset`), it figures out the changes introduced by a commit and creates a BRAND NEW commit that does the exact opposite.

Explain Like I'm New

If Commit A added the word 'Hello', `git revert` creates Commit B which deletes the word 'Hello'. The timeline moves forward, not backward.

Real World Example

You pushed a feature to the live website, and it immediately crashed the server. Because the commit is public, you cannot use `git reset`. You use `git revert <hash>` to instantly generate an 'Anti-Commit' that removes the broken code, and push that.

Common Use Cases

  • •Safely undoing public commits
  • •Maintaining a perfect audit trail

Terminal Output

bash / terminal
# Oh no! Commit 9f8e7d broke the production server! # It is already pushed to GitHub, so we must use revert. # Generate the Anti-Commit $ git revert 9f8e7d # Git will open your text editor with a default message like: # "Revert 'Add new payment gateway'" # Save and close the editor. # Push the fix to the server! $ git push origin main

Interview Questions

basic

  • Which is safer for public, shared branches: `reset` or `revert`?

intermediate

  • Does `git revert` delete the original broken commit from `git log`?

Flash Cards

Question

Safer?

Click to reveal answer
Answer

`git revert`. It never alters existing history, it only adds new history.

Question

Delete from log?

Click to reveal answer
Answer

No. The original broken commit stays in the history forever. Right above it in the log will be a new commit titled 'Revert: [broken commit name]'.