09/30/2026

Git Recovery in Seven Slides: Undo, Recover, Unbreak

The commands you need twice a year

Nobody memorizes the git commands for the bad day. This deck keeps them in one place: seven slides, each with the exact commands and what they do to your files. Use the arrow keys or the Next button. With scripts off, the slides read in order below.

Run the check on the first slide before you try anything else. Most recoveries start with what it shows.

Git recovery, slide by slide

  1. Git recovery: run git status, git stash list and git reflog first; a committed change stays recoverable for about 30 days, and edits you never committed are not covered
    Look firstgit status && git stash list && git reflog -20 answers three questions: what is uncommitted, what is set aside, and where HEAD has been. A committed change stays recoverable from the reflog for about 30 days by default (90 days while it is still reachable); edits you never committed are not covered.
  2. git reset HEAD~1 in three flavors: --soft keeps changes staged, --mixed keeps them unstaged, --hard discards them, overwrites files and drops uncommitted edits
    Three kinds of resetOnly --hard touches your files, and it discards uncommitted edits to tracked files too. --soft and --mixed move the branch pointer and leave the working tree as it is; with --mixed, a file the commit added becomes untracked.
  3. Reset moves the branch pointer back and drops a commit; revert adds a new commit that undoes it and keeps history
    Reset or revertOn anything you have pushed and others have pulled, use revert: it adds a commit instead of rewriting history.
  4. Recover lost work: run git reflog, create a branch at the lost sha, then switch to it; a branch keeps the commit reachable
    Reflog rescueFind the sha in the reflog, then pin it to a branch before you do anything else. A branch keeps the commit reachable. git switch needs Git 2.23 or newer.
  5. Committed to the wrong branch: git switch -c feature, then git branch -f main origin/main, then optionally git switch main; if already pushed, git revert the commit on main
    Wrong branchNot pushed yet: run git fetch, then git switch -c feature and git branch -f main origin/main. Edits you have not committed come along and no --hard is needed. Already pushed: moving main does nothing, so keep the work on the new branch and git revert the commit on main. git switch needs Git 2.23 or newer.
  6. Which undo do I need: not pushed and keep changes, git reset --soft HEAD~1; not pushed and throw it away, git reset --hard HEAD~1; already pushed, git revert <sha>; wrong branch, git switch -c feature then git branch -f main origin/main
    Which undo do I need?Pick the row that matches where you are. Not pushed, keep the changes: git reset --soft HEAD~1. Not pushed, throw it away: git reset --hard HEAD~1. Already pushed: git revert <sha>. Wrong branch, not pushed yet: git switch -c feature, then git branch -f main origin/main.
  7. Stuck in a rebase or merge: git rebase --abort and git merge --abort return the branch to where it was; to finish instead, git add the fixed files and run --continue
    Escape a rebase or mergegit status says which one you are in. Abort returns the branch to where it started; commit or stash before you begin, because uncommitted edits may not come back after a merge abort. To finish instead, fix the files, git add them and run git rebase --continue or git merge --continue.

Where the rest is

Stash, clean and the other ways to lose work quietly are on the Git recovery cheat sheet.

Researched and drafted with AI, then reviewed and edited by the site's editors.