Recovery by situation
| Situation | Command |
|---|---|
| Unstage | git restore --staged <file> |
| Restore working directory | git restore <file> |
| Amend the last commit | git commit --amend |
| Revert a commit (keep history) | git revert HEAD |
| Undo a commit (rewrite history) | git reset HEAD~1 |
| Stash work in progress | git stash |
| Recover a deleted commit | git reflog |
Unstaging / restoring the working directory
Bash
git restore --staged index.html
git restore index.html
git restore --staged .
git restore .Amending a commit (before push only)
Bash
git commit --amend -m "fix: correct message"
git add forgotten.txt
git commit --amend --no-editreset
Bash
git reset --soft HEAD~1 # undo commit only; changes stay staged
git reset HEAD~1 # undo commit and unstage; keep files
git reset --hard HEAD~1 # discard commit and all changes (dangerous)
git reset --hard abc1234revert (shared branches)
Bash
git revert abc1234
git revert --no-commit HEAD~3..HEAD
git commit -m "revert: undo the last 3 commits"stash
Bash
git stash
git stash push -m "login feature WIP"
git stash list
git stash pop
git stash apply stash@{1}
git stash drop stash@{0}
git stash branch feat/new stash@{0}reflog (recover deleted commits)
Bash
git reflog
git reset --hard HEAD@{3}
git reflog | grep "feat/login"
git checkout -b feat/login abc1234Editorial note
AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us
Topic-related official docs (auto-linked · not per-claim sources)Git documentation ↗
Questions & answers
Ask a question about this guide. We'll review and reply.