AI Hints
Home›Git & GitHub Tutorial›Undoing Commits
Git & GitHub · Lesson 30 of 144

Undoing a Commit Safely

Git revert.

⏱ 2 min read▶ Video at 31:37🎓 Free
Watch this lesson in the Git & GitHub courseWatch on YouTube ↗Full Git & GitHub course

Say a commit you made last week turned out to be a bad idea. You want to undo it. The safest way is git revert. It makes a new commit that does the exact opposite.

Undoing a Commit Safely
From the video: Undoing a Commit Safely
  • A bad commit, already saved
  • Undo it safely
  • git revert

git revert

The log shows our latest commit added a spammy sale banner. Git revert, dash dash no edit, and head, which means the latest commit. Git creates a new commit, called revert, add sale banner. The log shows both, the mistake and its undo. And the banner line is gone from the file.

git
git log --oneline -n 3
95c7f8d (HEAD -> main) Add sale banner
f63a17a Link to about page
ef9c947 Add basic styles
git revert --no-edit HEAD
[main b895bf3] Revert "Add sale banner"
 Date: Mon Jan 5 09:34:15 2026 +0000
 1 file changed, 1 deletion(-)
git log --oneline -n 3
b895bf3 (HEAD -> main) Revert "Add sale banner"
95c7f8d Add sale banner
f63a17a Link to about page
cat index.html
<h1>My Site</h1>
<p>Welcome!</p>
<a href="about.html">About</a>

Why revert is safe

Revert never deletes or changes history. It only adds a new commit on top. So it's completely safe, even for commits you've already shared with your team. Everyone can see what was undone, and why.

Adds a committhe exact opposite
History intactnothing deleted
Safe to shareeven after pushing
Any commitgit revert <hash>

Summary

git revert undoes a commit by adding a new opposite commit. It's the safe undo for shared history. Next, its more powerful, and more dangerous, cousin.

Key takeaways
  • git revert <hash>
  • Adds an undo commit
  • Safe for shared work
Next up: Moving Back with Reset — git reset