Everyone learning git hits the same moment. You run a command, something vanishes, and you're suddenly certain you've destroyed a week of work.
You almost certainly haven't. If you committed it at some point, git is very hard to lose work in. The problem is that every mistake has a different undo command, and nobody gives you the list.
So this is the list. Each mistake comes with the command that fixes it and whether it's still safe after you've pushed. It's worth one read-through now, while nothing is on fire.
First, stop typing
When something goes wrong, the worst thing you can do is paste three more commands from Stack Overflow. Run this instead:
git status
It tells you which branch you're on, whether a merge or rebase is half-finished, and what's changed. A lot of the time it also prints the exact command to back out of whatever just happened.
"I typed the wrong commit message"
If you haven't pushed yet:
git commit --amend -m "The message I meant"
Your last commit gets replaced by a new one with the right message.
"I forgot to add a file to that commit"
git add forgotten-file.txt
git commit --amend --no-edit
--no-edit keeps the message as it was. Again, only before you push.
"I want my last commit back as changes"
git reset --soft HEAD~1
The commit is gone, and everything that was in it is back as staged changes, ready to commit again differently. HEAD~1 means "one commit before where I am now."
"I staged a file I didn't mean to"
git restore --staged secrets.txt
The file on disk doesn't change. It just won't go into the next commit.
"I committed to main instead of a new branch"
It feels awful and it's easy to fix, as long as you haven't pushed:
git branch my-feature
git reset --hard HEAD~1
git switch my-feature
The first line makes a new branch pointing at your commit. The second moves main back by one commit. The third switches you to the new branch, where your commit is waiting.
Be careful with --hard. It throws away uncommitted changes. Run git status first and make sure you don't have any.
"This merge is a disaster, get me out"
In the middle of a merge with conflicts, and want to go back to before you started?
git merge --abort
For a rebase, it's git rebase --abort. Either way you end up exactly where you were.
"I reset and my commit is gone"
This is the one that convinces people they've lost work. They haven't. Git keeps a record of everywhere your branch has pointed, called the reflog:
git reflog
You'll see something like this:
a1b2c3d HEAD@{0}: reset: moving to HEAD~1
e4f5a6b HEAD@{1}: commit: Add the login form
The "lost" commit is right there, at HEAD@{1}. To go back to it:
git reset --hard HEAD@{1}
The reflog only exists on your machine, and old entries expire after a few months. A mistake from this afternoon will definitely still be in it.
"I already pushed it"
Once a commit is on the server, other people may already have it. If you rewrite it with --amend or reset, your next push gets refused, and forcing it can make a mess for your teammates. The safe undo is a new commit that reverses the old one:
git revert a1b2c3d
The history will show both the mistake and the fix. That's fine. Nobody's history is clean.
The cheat sheet
| Mistake | Fix | Safe after pushing? |
|---|---|---|
| Wrong commit message | git commit --amend | No |
| Forgot a file | git add then git commit --amend --no-edit | No |
| Take back last commit, keep changes | git reset --soft HEAD~1 | No |
| Staged the wrong file | git restore --staged <file> | Yes, nothing was committed |
| Committed to the wrong branch | git branch, git reset --hard HEAD~1, git switch | No |
| Bad merge in progress | git merge --abort | Yes |
| Lost a commit to reset or rebase | git reflog, then git reset --hard HEAD@{n} | Yes, it's local |
| Bad commit already pushed | git revert <sha> | Yes |
Or press Ctrl+Z
These commands are worth learning, since they'll work on every computer you ever touch. While you're still learning them, though, a safety net that doesn't make you pick the right one of eight is nice to have.
I maintain a free desktop git client called Git Revenge, and undo is the first thing I'd show a beginner. After a commit, checkout, merge, cherry-pick, rebase, reset, revert or pull, press Ctrl+Z and it's reversed. Ctrl+Shift+Z puts it back.
Before you press anything, the command palette (Ctrl+K) shows what the next undo will reverse, something like Undo: Commit "Add the login form".
If what you're undoing is already on the server, the app stops, lists the commits, and offers Revert instead. That's the safe option from the table above.
If anything has moved since, even from a terminal, undo refuses rather than guess. I'd rather it say no than reverse the wrong thing.
When Ctrl+Z can't help, Show the reflog… in the command palette lists everywhere your branch has been, so you can find the "lost" commit and get it back.
Your branches are drawn as a graph, too. Run a reset or rebase and watch the branch move. It's a quicker way to see what those commands do than reading about them.
Before you install it
Install git first. Git Revenge uses the git on your computer. On Windows, get it from git-scm.com. On a Mac, run brew install git with Homebrew, since the version Apple ships is too old.
It doesn't show you the command behind each button. Keep this post open while you use it and try the command version too. You want to know both.
It's free, with no account or trial, so you don't need a student discount. It runs on Windows, macOS and Linux. The Undo a mistake walkthrough goes through a recovery step by step, with screenshots.






