Git Undo Cheatsheet — reset / revert / restore
Git undo operations are among the most frequently used — and most confusing — commands for developers. This guide compares reset, revert, and restore across different scenarios so you always know how to undo your last action.
Quick Decision Guide
| Scenario | Command | Recommendation |
| Undo last commit, keep changes to redo | git reset --soft HEAD~1 | ⭐⭐⭐⭐⭐ |
| Completely discard last commit's changes | git reset --hard HEAD~1 | ⭐⭐ Use with caution |
| Undo a pushed commit safely | git revert | ⭐⭐⭐⭐⭐ |
| Discard uncommitted changes in working tree | git restore | ⭐⭐⭐⭐⭐ |
| Unstage a file (keep changes) | git restore --staged | ⭐⭐⭐⭐⭐ |
Core Commands
### git reset — Move HEAD Pointer
reset has three modes, differing in how they affect the staging area and working directory:
| Mode | HEAD | Staging | Working Dir | Safety |
--soft | ✅ Moves | ✅ Kept | ✅ Kept | 🟢 Safe |
--mixed (default) | ✅ Moves | ❌ Cleared | ✅ Kept | 🟡 Caution |
--hard | ✅ Moves | ❌ Cleared | ❌ Discarded | 🔴 Dangerous |
revert doesn't rewrite history — it creates a new commit that inverses the changes from a previous commit. This is the only safe way to undo pushed commits.
``bash
# Undo a single commit
git revert abc1234
# Undo a range of commits git revert HEAD~3..HEAD
# Revert without auto-committing (review first)
git revert --no-commit abc1234
`
### git restore — Discard Uncommitted Changes
`bash
# Discard working directory changes for a file
git restore src/app.ts
# Discard all working directory changes git restore .
# Unstage a file (keep working directory changes) git restore --staged src/app.ts ``
Undo(19)
| Command | Level | ||
|---|---|---|---|
git commit --amendAmend the most recent commit message or content | Basic | git commit --amend -m "New message" | |
git reset --softSoft reset, keep changes staged | Intermediate | git reset --soft HEAD~1 | |
git reset --mixedMixed reset, keep changes in working directory | Intermediate | git reset --mixed HEAD~1 | |
git reset --hardHard reset, discard all changes | Intermediate | git reset --hard HEAD~1 | |
git revertRevert the changes introduced by a commit (creates a new commit) | Intermediate | git revert HEAD | |
git revert --no-commitRevert changes without auto-committing | Intermediate | git revert --no-commit HEAD | |
git restoreRestore working tree files to a specified state | Basic | git restore file.txt | |
git restore --stagedUnstage files from the staging area | Basic | git restore --staged file.txt | |
git restore .Restore all working tree files (discard all changes) | Basic | git restore . | |
git restore --sourceRestore files from a specific commit | Intermediate | git restore --source HEAD~2 file.txt | |
git checkout --Discard changes in working directory files | Basic | git checkout -- file.txt | |
git checkout -fForce switch branches discarding local changes | Basic | git checkout -f main | |
git clean -fdForce remove untracked files and directories | Intermediate | git clean -fd | |
git clean -nDry-run to show which untracked files would be removed | Basic | git clean -n | |
git clean -fdXRemove ignored files and directories | Intermediate | git clean -fdX | |
git reflogShow the reference log (all HEAD movement history) | Intermediate | git reflog | |
git rebase --abortAbort an in-progress rebase operation | Intermediate | git rebase --abort | |
git cherry-pick --abortAbort an in-progress cherry-pick operation | Intermediate | git cherry-pick --abort | |
git bisect resetReset the bisection state | Intermediate | git bisect reset |