← Cheat Sheets
Git Undoing Changes
Safe ways to undo work, from unstaging to reverting.
# Git Undoing Changes
Safe ways to undo work, from unstaging to reverting.
| Goal | Command |
|---|---|
| Discard changes in a file | `git restore file` |
| Unstage a file | `git restore --staged file` |
| Undo last commit, keep changes staged | `git reset --soft HEAD~1` |
| Undo last commit, keep changes unstaged | `git reset --mixed HEAD~1` |
| Undo last commit and discard changes | `git reset --hard HEAD~1` |
| Change last commit message or add files | `git commit --amend` |
| Undo a pushed commit safely | `git revert <sha>` |
| Remove untracked files (dry run first) | `git clean -n` then `git clean -fd` |
| Find lost commits | `git reflog` |
**Rule of thumb:** never rewrite history that others have pulled. Use `revert` for shared branches.