Undo Local Changes
Git · Example / how-to
Undo Local Changes
Git · Example / how-to
📋 Overview
Discard or unstage local work safely: restore files, unstage the index, or reset unpushed commits without rewriting remote history.
🔧 Core concepts
| Piece | Role |
|---|---|
restore | Discard working tree / unstage |
reset --soft | Move HEAD, keep changes staged |
reset --mixed | Move HEAD, keep changes unstaged |
clean | Remove untracked files |
💡 Examples
Unstage a file (keep edits):
git restore --staged path/to/file.tsDiscard unstaged edits to a file:
git restore path/to/file.tsUndo last local commit, keep changes staged:
git reset --soft HEAD~1Undo last local commit, keep changes unstaged:
git reset HEAD~1Remove untracked files (dry run first):
git clean -nd
git clean -fd⚠️ Pitfalls
git restore/clean -fdpermanently deletes uncommitted work — dry-run first.reset --hardis destructive; avoid unless you truly want a clean tree.- Do not reset commits that exist on the remote shared branch.