When a Git mistake happens the first instinct is often panic. A calm recovery plan keeps valuable work intact. This guide collects the commands I rely on when something breaks, written in plain language so you can follow it under pressure.
Start With a Safety Snapshot
Before touching commits, copy the current state. Create a temporary branch so nothing is lost while you experiment.
git switch -c rescue/snapshot-$(date +%s)You can now return to the main branch and continue the rescue with a backup ready.
Scenario 1: Uncommitted Work Vanished
Sometimes files disappear after a checkout or reset. Recovery depends on whether the work was ever committed.
Check the stash list first — stashes preserve uncommitted changes:
git stash list
git stash show -p stash@{0}If the change is there, apply it and drop the entry once verified.
git stash apply stash@{0}If the work was committed before it vanished, inspect the reflog. It records every move your HEAD made.
git reflogFind the commit before the loss and check it out temporarily.
git switch --detach <hash>Copy the necessary files back into your working tree, then return to your branch.
git switch mainIf the work was never committed or stashed, Git may not have it. Check your editor's local history, any backup, or as a last resort:
git fsck --lost-foundThat can surface dangling blobs from orphaned objects — not a guarantee, but worth trying before you rewrite anything.
Scenario 2: Wrong Commit on Main
When an accidental commit reaches the main branch, decide whether it has been shared.
If nobody else pulled it, use a soft reset to keep the changes staged.
git reset --soft HEAD~1Fix the files and recommit.
If the commit was pushed, use git revert so collaborators keep a clean history.
git revert <hash>This creates a new commit that undoes the bad one, leaving a clear trail of what happened.
Scenario 3: Tracking Down a Production Bug
The fastest way to locate a regression is git bisect. It performs a binary search through history.
Start the session by marking a good and a bad revision.
git bisect start
git bisect bad HEAD
git bisect good <known-good-hash>Git checks out a midpoint. Test the build; if the bug appears run git bisect bad, otherwise git bisect good. Repeat until Git prints the offending commit. Finish the session with git bisect reset to return to your branch.
Scenario 4: Multiple Tasks on One Branch
Working on unrelated features inside one branch slows everyone down. Use git worktree to create a separate workspace without cloning the repository again.
git worktree add ../feature-login login-formThe command above creates a new folder ../feature-login checked out on branch login-form. You can switch between directories and work independently, avoiding messy stash juggling.
Remove the workspace when the task ends.
git worktree remove ../feature-loginScenario 5: Merge Conflicts That Keep Returning
Large conflicts can reappear whenever you rebase or merge. Teach Git to remember your resolutions. Enable rerere once and Git can replay a resolution when the same conflict hunk shows up again.
git config --global rerere.enabled trueWhen a matching conflict appears again, Git applies the previous resolution automatically. Always verify the result before committing.
Useful Commands That Often Rescue Me
git restore --staged <file> unstages a file — it removes it from the index while keeping your edits in the working tree. Use it when you git added too early.
git reset --patch lets you unstage portions of a file. It is ideal when you accidentally bundled multiple changes into one commit.
git commit --amend rewrites the most recent commit. Use it for fixing typos or adding forgotten files before you share the branch.
git clean -n previews which untracked files Git will delete. Run git clean -f only after you feel safe with the preview.
Decision Table
Final Checklist
- Snapshot the repository before changing history.
- Recover lost edits from stash, local history, or reflog (for commits) before editing new files.
- Choose reset or revert based on whether the commit is public.
- Use bisect for fast regression hunts.
- Lean on worktrees and rerere to keep multi-branch work smooth.
Store this guide somewhere accessible. In a stressful moment, having a calm plan is the difference between losing hours of work and rescuing everything in minutes.
