shellstreak

git reset vs revert vs restore (and how to undo the last commit)

Updated · Every command on this page ran in our practice shell

Short answer

git restore fixes files (discard edits or unstage them), git revert adds a new commit that undoes an old one, and git reset moves your branch back to an earlier commit. To undo the last commit safely, use git revert HEAD if it's shared, or git reset --soft HEAD~1 to keep its changes staged. git reset --hard HEAD~1 also throws the changes away.

Git has three undo commands with confusingly similar names. The short version: restore fixes files, revert undoes a commit by adding a new one, and reset moves your branch back in time. Which one is right depends on whether the mistake is committed, and whether anyone else already has it.

Discard changes to a file with git restore

git restore file puts a file back to how it was in the last commit (or the staging area), throwing away your unstaged edits. It also brings back a file you deleted by accident. The discarded edits are gone for good, so check git diff file first.

example
learner@shellstreak:~/kiosk$ git restore src/config.js && git status -s
 M src/cart.js
 D tests/cart.test.js
?? src/discount.js

Unstage a file with git restore --staged

git restore --staged file takes a file out of the next commit but keeps your edits in the working tree. Nothing is lost. The older spelling is git reset file. Going the other way, git commit -am "message" stages every change to tracked files and commits it in one step.

example
learner@shellstreak:~/shop$ git restore --staged notes.txt && git status -s
 M notes.txt

Undo a commit safely with git revert

git revert <commit> creates a new commit that does the opposite of the old one. History isn't rewritten, so it's the right choice for commits you've already pushed or shared. In a real terminal it opens your editor for the message; --no-edit keeps git's Revert "..." message. (Stuck in vim? Press Esc, type :wq, press Enter.)

example
learner@shellstreak:~/kiosk$ git revert --no-edit HEAD && git log --oneline
[agent/discounts 9bb9728] Revert "Speed up test script"
 Date: Mon Mar 2 09:31:00 2026 +0000
 2 files changed, 5 insertions(+), 1 deletion(-)
 create mode 100644 tests/cart.test.js
9bb9728 (HEAD -> agent/discounts) Revert "Speed up test script"
bd00b40 Speed up test script
edc216d Add discount codes
d152984 (main) Add cart tests
bd2bc24 Create kiosk app

Move back but keep changes: git reset --soft

git reset --soft HEAD~1 moves your branch back one commit and leaves that commit's changes staged. Use it to redo a commit you just made: fix the files or the message, then commit again.

example
learner@shellstreak:~/kiosk$ git reset --soft HEAD~1 && git status -s
M  src/cart.js
M  src/config.js
A  src/discount.js
D  tests/cart.test.js

The default: git reset --mixed

git reset HEAD~1 (mixed mode, the default) moves the branch back and unstages the changes, but keeps them in your files. You get to re-stage exactly what you want.

example
learner@shellstreak:~/kiosk$ git reset HEAD~1
Unstaged changes after reset:
M	src/cart.js
M	src/config.js
D	tests/cart.test.js

Throw it all away: git reset --hard

git reset --hard HEAD~1 moves the branch back and overwrites your files and staging area to match. The commit's changes and any uncommitted edits disappear. Committed work can sometimes be recovered with git reflog; uncommitted work can't. Always run git status first.

example
learner@shellstreak:~/kiosk$ git status -s
 M src/cart.js
 M src/config.js
 D tests/cart.test.js
?? src/discount.js

Undo the last commit

Pick by situation:

  • Already pushed or shared: git revert --no-edit HEAD. Safe, keeps history.
  • Not pushed, keep the changes staged: git reset --soft HEAD~1.
  • Not pushed, keep the changes unstaged: git reset HEAD~1.
  • Not pushed, throw the changes away: git reset --hard HEAD~1. Destructive.
  • Only the message is wrong: git commit --amend -m "better message".

Never reset a branch other people have pulled and then git push --force it: their copies no longer match yours.

example
learner@shellstreak:~/kiosk$ git log --oneline -n 2
b02e6b8 (HEAD -> main) Add discount codes
d152984 Add cart tests

Check yourself

One real exercise from the “Supervising an AI coding agent” unit. No account needed.

The agent's commit Add discount codes hasn't been pushed. Undo the commit, but keep all its changes staged so you can fix them before committing again.

What's checked: the repository's commits, staged changes, unstaged changes.

Common mistakes

  • Using git reset --hard when git restore file would have fixed just the one file.

  • Resetting a commit that's already pushed, then force-pushing over everyone else's history. Use git revert instead.

  • Running git restore file to unstage and losing the edit. Unstaging needs --staged.

  • Forgetting that git reset --hard also wipes uncommitted edits in other files, not just the commit.

Questions

What's the difference between git reset and git revert?+

`reset` moves the branch back, removing commits from its history. `revert` keeps history and adds a new commit that undoes an old one. Revert is safe for shared branches; reset is for local work.

Can I get back a commit after git reset --hard?+

Usually, yes: `git reflog` lists where `HEAD` has been, and `git reset --hard <hash>` jumps back. Edits that were never committed can't be recovered this way.

Is git checkout -- file the same as git restore file?+

Yes. `git restore` was added in Git 2.23 to split file operations out of the overloaded `git checkout`. Both discard unstaged edits.

Reading is step one. Now make it stick.

Practise this in a five-minute lesson with instant feedback. No account needed.

Practise it now