Skip to content

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.

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 moves your branch pointer backwards. It rewrites history. Fine on your own unpushed branch.
  • revert makes a new commit that undoes an old one. History stays intact. The only polite option on shared branches like main.

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.

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:

Terminal window
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.

Committed to main instead of a feature branch, not pushed yet:

Terminal window
git branch feature/oops # new branch points at your commits
git reset --hard origin/main # move main back to where the remote is
git switch feature/oops # your work is here

Sometimes you do need to rewrite a branch you pushed: your own feature branch after a rebase, for example.

Terminal window
git push --force-with-lease

Never 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.

<<<<<<< HEAD
listen 8080;
=======
listen 8081;
>>>>>>> feature/port-change

Top is yours, bottom is theirs. Edit the file to what it should be, delete the markers, then:

Terminal window
git add nginx.conf
git commit # or: git rebase --continue, if you were rebasing

Lost 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.

Then, if it wasn’t pushed yet:

Terminal window
git rm --cached .env
echo ".env" >> .gitignore
git commit --amend # if it was the last commit

If 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.

When the local repo is truly cursed and everything worth keeping is pushed:

Terminal window
cd ..
mv repo repo-cursed
git clone git@github.com:you/repo.git

Copy 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.