Home/Engineer/Git / CI·CD/Undoing Git Mistakes: Recove…
Git / CI·CDIntermediateubuntumacoswindowsgitresetrevert

Undoing Git Mistakes: Recovering Bad Commits, Wrong-Branch Work, and Deleted Files with reset, revert, stash, and reflog

From bad commits and work on the wrong branch to files you deleted by accident — a situation-by-situation guide to undoing Git mistakes.

Contents
  1. Recovery by situation
  2. Unstaging / restoring the working directory
  3. Amending a commit (before push only)
  4. reset
  5. revert (shared branches)
  6. stash
  7. reflog (recover deleted commits)

Recovery by situation

SituationCommand
Unstagegit restore --staged <file>
Restore working directorygit restore <file>
Amend the last commitgit commit --amend
Revert a commit (keep history)git revert HEAD
Undo a commit (rewrite history)git reset HEAD~1
Stash work in progressgit stash
Recover a deleted commitgit 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-edit

reset

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 abc1234

revert (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 abc1234
#git#reset#revert#stash#reflog
Editorial 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.