Learn / Git

git reset vs revert vs restore: which undo to use

Three commands, three jobs. restore fixes files, reset moves your branch back, revert cancels a commit that is already shared. Pick by asking one question: did I push it?

The short answer

CommandWhat it changesSafe after you push?
git restoreFiles in your folder, or what is stagedYes, it never touches commits
git resetWhere your branch points: it removes commits from the branchNo. Use it only on commits nobody else has
git revertAdds a new commit that cancels an old oneYes. This is the safe way to undo shared work

git restore: undo changes to files

git restore cart.js                  # throw away unstaged edits (gone for good)
git restore --staged cart.js         # unstage the file, keep your edits
git restore --source=HEAD~2 cart.js  # get the file as it was 2 commits ago

You will also see older forms of the same thing: git checkout -- file and git reset HEAD file. They still work. restore is the clearer name.

git reset: move your branch back

Reset moves your branch to an earlier commit. The mode decides what happens to the changes from the commits you step back over:

git reset --soft HEAD~1       # undo last commit, changes stay staged
git reset HEAD~1              # --mixed (the default): changes stay, unstaged
git reset --hard HEAD~1       # undo the commit AND delete the changes
git reset --hard origin/main  # make my branch match GitHub exactly
ModeCommitStaging areaYour filesUse when
--softundonekeptkeptRedo the commit differently
--mixedundoneclearedkeptChoose again what to stage
--hardundonecleareddeletedThrow everything away

Reset rewrites history. If the commits are already on GitHub, your branch and the team's copy no longer agree, and the only way to push is a force push that breaks everyone else's copy. For pushed commits, use revert.

git revert: undo a pushed commit safely

git revert 3f9c2a1       # new commit that reverses 3f9c2a1
git revert -m 1 8e7d6c5  # reverting a merge commit: keep parent 1
git push                 # share the fix like any other commit

The bad commit stays in history, and a new commit cancels it. Nobody's copy breaks, which is why teams use revert to roll back a bad change on a live system. On GitHub, a merged pull request also has a Revert button that opens a new pull request doing the same thing.

“I think I lost it”: git reflog

Git keeps a diary of everywhere your HEAD has been. A hard reset is usually not the end:

git reset --hard HEAD~3   # oh no
git reflog                # find the commit you were on
b71e004 HEAD@{0}: reset: moving to HEAD~3
3f9c2a1 HEAD@{1}: commit: feat: add cart total
git reset --hard 3f9c2a1  # back. Or: git branch rescue 3f9c2a1

By default, reflog entries last about 90 days, but only about 30 for commits no branch points to any more, which is the usual case right after a reset. The reflog only lives on your computer. And it cannot bring back edits you never committed: those are gone after restore or reset --hard. Commit often.

Which one do I need?

SituationFix
Edited a file and want the original backgit restore file
Staged the wrong filegit restore --staged file
Typo in the last commit message (not pushed)git commit --amend -m "new message"
Forgot a file in the last commit (not pushed)git add file, then git commit --amend --no-edit
Undo the last commit, keep the work (not pushed)git reset --soft HEAD~1
Bad commit already pushedgit revert <sha>, then push
Committed on main by mistake (not pushed)git switch -c feature/x, git switch main, git reset --hard origin/main
Lost commits after a reset or a bad rebasegit reflog, then git reset --hard <sha>
Committed a password or keyChange the password or key first. Removing it from history comes second

The interview answer

“Reset moves the branch back and rewrites history, so I only use it on commits I haven't shared. Revert adds a new commit that cancels the old one, so it is safe for anything already pushed. Restore works on files, not commits.”

Push failing after you fixed things? See git push rejected (fetch first).

From the book

Git / GitHub Study Guide

151 pages for beginners: every command explained, 4 local drills with a PASS or FAIL checker, 4 GitHub labs written as work tickets and GH-900 exam prep.