Updated
Almost nothing in Git is truly lost. The trick is choosing the right undo.
Discard changes you haven’t staged
git restore path/to/file # back to the last commit's version
git restore . # every file (careful!)
Unstage (keep the edits)
git restore --staged path/to/file
Fix the last commit
Forgot a file or made a typo in the message? As long as you haven’t pushed:
git add forgotten-file
git commit --amend # opens the editor; or add -m "new message"
Undo commits with reset (local only)
git reset --soft HEAD~1 # undo commit, keep changes staged
git reset --mixed HEAD~1 # undo commit, keep changes unstaged (default)
git reset --hard HEAD~1 # undo commit AND throw away the changes
HEAD~1 means “one commit before the current one”. Use reset only on commits you haven’t shared.
Undo a pushed commit with revert
revert makes a new commit that does the opposite. History isn’t rewritten, so it’s safe on shared branches:
git revert 3f2a1c9
To revert a merge commit, tell Git which parent to keep (usually 1, the branch you merged into):
git revert -m 1 <merge-commit>
Recover “lost” commits with reflog
Did a reset --hard or a deleted branch eat your work? The reflog records everywhere HEAD has been:
git reflog
# a1b2c3d HEAD@{2}: commit: The work I thought I lost
git switch -c rescue a1b2c3d
Reflog entries are kept for about 90 days by default.
Cheat table
| I want to… | Command |
|---|---|
| Throw away edits to a file | git restore <file> |
| Unstage a file | git restore --staged <file> |
| Change the last (unpushed) commit | git commit --amend |
| Remove unpushed commits | git reset |
| Undo a pushed commit | git revert <hash> |
| Find something I lost | git reflog |
One Git tip a week
Short, practical, no spam. Get the free printable Gitflow cheat sheet when you join.
Newsletter launching soon.