Git undo cheat sheet: choose by file state
Undo Git changes safely by identifying whether work is untracked, modified, staged, committed locally, or already published.
Start with git status, git diff, and git diff --cached. The safest command depends on where the change lives.
| State | Goal | Command |
|---|---|---|
| Staged tracked file | Unstage, keep content | git restore --staged <path> |
| Modified tracked file | Discard one file | Back up, then git restore <path> |
| Last local commit | Keep changes staged | git reset --soft HEAD~1 |
| Published commit | Record an inverse | git revert <commit> |
| Branch moved accidentally | Find old tip | git reflog |
Preserve first
git status
git diff
git diff --cached
git branch backup/before-undo
For uncommitted work, a normal filesystem copy outside the repository is an additional independent safety net.
Amend the latest unpublished commit
git add path/to/correction
git commit --amend
Amend rewrites the last commit. Avoid amending a commit others already use unless the team explicitly coordinates the history change.
Recover a previous pointer
git reflog
git branch recovery/found-work <commit>
Create the recovery branch before trying more history edits.
High risk:
git reset --hard,git clean -fd, andgit checkout -- .can destroy uncommitted work. Never paste them blindly from an error page. Preview removable untracked files withgit clean -nd, make a backup, and target exact paths whenever possible.
For the history model, read Git reset vs. revert.