Undo strategies depend on what you already shared
If a commit exists only on your machine, history rewriting (reset, amend) is usually fine. If others already pulled the commit, prefer git revert to add a compensating commit.
Undo unstaged edits
git restore path/to/file
# older Git:
git checkout -- path/to/file
Unstage files
git restore --staged path/to/file
git reset HEAD path/to/file
Undo the last commit but keep changes
git reset --soft HEAD~1 # keep staged
git reset --mixed HEAD~1 # keep working tree, unstage
Discard the last commit and changes (dangerous)
git reset --hard HEAD~1
Only use hard reset when you are sure the changes are disposable.
Undo a commit already pushed
git revert HEAD
git push
Recover with reflog
git reflog
git reset --hard HEAD@{2}
Reflog is your safety net for recent HEAD movements on a local repo.
Amend the last commit message or files
git commit --amend
Do not amend commits that are already on a shared main branch unless your team explicitly allows force-push workflows.
Practical rule of thumb
- Working tree mess →
restore - Wrong staging → unstage
- Local commit mistake → soft/mixed reset
- Shared history mistake → revert
- “I lost a commit” → reflog



