Code Reference

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

PieceRole
restoreDiscard working tree / unstage
reset --softMove HEAD, keep changes staged
reset --mixedMove HEAD, keep changes unstaged
cleanRemove untracked files

💡 Examples

Unstage a file (keep edits):

git restore --staged path/to/file.ts

Discard unstaged edits to a file:

git restore path/to/file.ts

Undo last local commit, keep changes staged:

git reset --soft HEAD~1

Undo last local commit, keep changes unstaged:

git reset HEAD~1

Remove untracked files (dry run first):

git clean -nd
git clean -fd

⚠️ Pitfalls

  • git restore / clean -fd permanently deletes uncommitted work — dry-run first.
  • reset --hard is destructive; avoid unless you truly want a clean tree.
  • Do not reset commits that exist on the remote shared branch.

On this page