git revert & git reset

Undo a commit: revert and reset

Lesson 17 of 20 · 11 min · Module 5: Teamwork and fixing mistakes

Commits can be undone too. The right tool depends on one question: has anyone else got the commit yet?

Start this lesson →

Free. No installation: you write and run the code in your browser, and it is checked automatically.

By the end, you’ll be able to…

  • Undo a shared commit with git revert
  • Undo local commits with git reset
  • Recover with git reflog

The idea

git revert commit creates a new commit with the opposite changes. The history keeps both, so it is the safe way to undo a commit that is already pushed. git reset HEAD~1 moves your branch back one commit, as if it never happened; the changes stay in your files, unstaged. --soft keeps them staged, and --hard throws them away. Reset only commits you have not pushed. If a reset goes too far, git reflog remembers where HEAD has been.

git revert HEAD
git reset HEAD~1
git reflog

01revert: undo with a new commit

git revert creates a commit with the opposite changes. Nobody's history is rewritten, so it is safe for commits that are already pushed. On your computer, Git opens your editor with a ready message; the practice terminal accepts it for you.

git revert HEAD~1
Output
[main 5027bc0] Revert "Add a pop-up banner"
 1 file changed, 1 deletion(-)
 delete mode 100644 banner.html

02reset: move the branch back

git reset HEAD~1 moves the branch back one commit. Its changes stay in your files, unstaged, so you can rework them. --soft keeps them staged; --hard throws them away.

git reset HEAD~1
git status --short
Output
Unstaged changes after reset:
M	style.css
 M style.css

03The reflog remembers

git reflog lists every commit HEAD pointed to, even ones no branch points to any more. After a reset that went too far, git reset --hard HEAD@{1} goes back to where you were.

git reset --hard HEAD~1
git reflog -3
Output
HEAD is now at 93cc509 Add a pop-up banner
93cc509 (HEAD -> main, origin/main, origin/HEAD) HEAD@{0}: reset: moving to HEAD~1
8e7e025 HEAD@{1}: commit: Try a new font
93cc509 (HEAD -> main, origin/main, origin/HEAD) HEAD@{2}: commit: Add a pop-up banner

Your turn

Two mistakes to fix. Your latest commit, Try a new font, exists only on this computer: undo the commit with git reset, but keep the change in style.css to finish later. The commit before it, Add a pop-up banner, is already on GitHub and visitors hate it: undo it with git revert and push the revert. Leave the font change uncommitted.

What the checks look for

  • The font commit is gone, but its change is still in style.css
  • The banner is undone by a revert commit
  • GitHub has the revert and keeps the original in its history
  • The font change was never pushed
Try it in the editor →

Take this with you

Pushed? Revert it. Only on your computer? Reset it.

Go a little further

Run git reset --hard HEAD~1, then find the lost commit in git reflog and bring it back with git reset --hard HEAD@{1}.

Learn Git & GitHub for free

20 short lessons with real code and instant checks.

See the whole course →