Skip to content

Undoing Changes

bparlaker edited this page Oct 6, 2026 · 2 revisions

Undoing Changes

"How do I undo…?" is the most common Git question. The right command depends on where the change is and whether it's been pushed.

Decision table

Where is the mistake? Command Rewrites history?
Edited a file, not staged git restore <file> No
Staged a file by accident git restore --staged <file> No
Last commit, not pushed (message/typo/missing file) git commit --amend Yes (local only)
Last n commits, not pushed git reset --soft HEAD~n / --mixed / --hard Yes (local only)
Commit already pushed git revert <sha> No - adds a new "inverse" commit
"I lost a commit!" git reflog then git switch -c rescue <sha> No

reset modes compared

Assume HEAD is at commit C and we run git reset <mode> HEAD~1 (back to B):

Mode Branch pointer Staging area Working files
--soft moves to B keeps C's changes staged untouched
--mixed (default) moves to B reset - changes unstaged untouched
--hard moves to B reset reset - changes lost ⚠️

Examples

# Forgot to add a file to the last commit
git add forgotten.md
git commit --amend --no-edit

# Undo a bad commit that is already on GitHub
git revert 3b9f0d1
git push

# Recover after an accidental reset --hard
git reflog
# 1e0d5c3 HEAD@{0}: reset: moving to HEAD~1
# 7c1e2a9 HEAD@{1}: commit: docs: add labels page   <- the "lost" commit
git reset --hard 7c1e2a9

Golden rule

Don't rewrite history that others have pulled. On shared branches, use git revert. Use amend/reset/rebase only on commits that exist solely on your machine (or your own feature branch). If you must force-push your own branch, use git push --force-with-lease, which refuses if someone else pushed in the meantime.

git reflog keeps a record of where HEAD has been (by default ~90 days), so it is very hard to truly lose committed work.

Clone this wiki locally