Undo Unstaged Changes
# Discard changes in a specific file
git restore file.js
# Discard ALL unstaged changes
git restore .
Undo Staged Changes
# Unstage a file (keep changes in working dir)
git restore --staged file.js
# Unstage everything
git restore --staged .
Undo Last Commit
# Undo commit, keep changes staged
git reset --soft HEAD~1
# Undo commit, unstage changes
git reset HEAD~1
# Undo commit AND discard changes (DANGEROUS)
git reset --hard HEAD~1
Fix Last Commit Message
git commit --amend -m "new message"
Undo a Push
# Create a new commit that undoes the bad one (SAFE)
git revert <commit-hash>
git push
# Force push to remove commit (DANGEROUS on shared branches)
git reset --hard HEAD~1
git push --force-with-lease
Recover Lost Commits with Reflog
# View reflog — shows ALL recent actions
git reflog
# Restore to a specific point
git reset --hard HEAD@{3}
Decision Tree
| Situation | Command |
| Discard local file changes | git restore <file> |
| Unstage a file | git restore --staged <file> |
| Undo last commit (keep changes) | git reset --soft HEAD~1 |
| Undo pushed commit safely | git revert <hash> |
| Fix commit message | git commit --amend |
| Find lost commit | git reflog |