Recovering from Mistakes
Good news: git almost never truly loses committed work. Bad news: “almost never” carries a lot of weight in that sentence, and uncommitted work isn’t covered at all.
Rule zero: before you try a fix you found online, make a safety branch: git branch backup-before-i-make-it-worse. It costs nothing, and you can’t make it worse.
Undo matrix
Section titled “Undo matrix”| I want to… | Command |
|---|---|
| Discard changes to a file (not staged) | git restore file (gone forever) |
| Unstage a file (keep the changes) | git restore --staged file |
| Fix the last commit’s message / add a forgotten file | git add forgotten && git commit --amend |
| Undo the last commit, keep the changes | git reset --soft HEAD~1 |
| Undo the last commit, throw away the changes | git reset --hard HEAD~1 |
| Undo a commit that’s already pushed | git revert <sha> |
| Put work aside for a minute | git stash / git stash pop |
reset vs revert
Section titled “reset vs revert”resetmoves your branch pointer backwards. It rewrites history. Fine on your own unpushed branch.revertmakes a new commit that undoes an old one. History stays intact. The only polite option on shared branches likemain.
If it’s been pushed and someone else might have pulled it, revert. Every time. Rewriting shared history turns your mistake into everyone’s afternoon.
The reflog: git’s black box recorder
Section titled “The reflog: git’s black box recorder”You did a reset --hard, deleted a branch, or a rebase went sideways, and your commits are “gone.” They aren’t. The reflog records every place HEAD has been:
git reflog# a1b2c3d HEAD@{0}: reset: moving to HEAD~3# e4f5g6h HEAD@{1}: commit: the work you thought you lost# ...
git branch rescue e4f5g6h # put a branch on it. Saved.Reflog entries stick around for ~90 days by default. This is the most important command on this page. Remember it exists.
Pushed to the wrong branch
Section titled “Pushed to the wrong branch”Committed to main instead of a feature branch, not pushed yet:
git branch feature/oops # new branch points at your commitsgit reset --hard origin/main # move main back to where the remote isgit switch feature/oops # your work is hereForce pushing
Section titled “Force pushing”Sometimes you do need to rewrite a branch you pushed: your own feature branch after a rebase, for example.
git push --force-with-leaseNever plain --force. --force-with-lease refuses if someone else pushed in the meantime, so you don’t silently erase their work. And never force-push main. Turn on branch protection so you can’t, even at 3 AM, even when you’re sure.
Merge conflicts
Section titled “Merge conflicts”<<<<<<< HEADlisten 8080;=======listen 8081;>>>>>>> feature/port-changeTop is yours, bottom is theirs. Edit the file to what it should be, delete the markers, then:
git add nginx.confgit commit # or: git rebase --continue, if you were rebasingLost and want out? git merge --abort or git rebase --abort puts everything back the way it was. No shame. I’ve aborted more rebases than I’ve finished.
I committed a secret
Section titled “I committed a secret”Then, if it wasn’t pushed yet:
git rm --cached .envecho ".env" >> .gitignoregit commit --amend # if it was the last commitIf it was pushed, removing it from history requires a rewrite with git filter-repo (not the old filter-branch), a force push, and everyone re-cloning. That’s a lot of work, and it doesn’t un-leak the secret. That’s why rotation comes first. History cleanup is just hygiene.
Nuclear option
Section titled “Nuclear option”When the local repo is truly cursed and everything worth keeping is pushed:
cd ..mv repo repo-cursedgit clone git@github.com:you/repo.gitCopy anything uncommitted out of repo-cursed, then delete it once you’re sure. There’s an XKCD about this exact workflow. It’s a documentary.