Made a mistake in Git? Don't panic — almost everything is recoverable. This cheat sheet maps the most common "oh no" moments to the exact command that fixes them, organized by where things went wrong: your last commit, staging, your working directory, branches, and full-blown emergencies.
Before you do anything, run git status. It tells you exactly what state
your repo is in, so you fix the right problem.
I messed up my LAST COMMIT
| What happened | Fix it with |
|---|---|
| Committed too early, want to undo but keep changes | git reset --soft HEAD~1 |
| Committed to the wrong branch, want to move the commit | git reset --soft HEAD~1, then git stash and switch branches |
| Commit message has a typo | git commit --amend -m "Correct message" |
| Forgot to add a file to the commit | git add forgotten-file<br>git commit --amend --no-edit |
| Want to completely delete the last commit (permanent!) | git reset --hard HEAD~1 |
I messed up STAGING (git add)
| What happened | Fix it with |
|---|---|
| Added a file by accident | git reset HEAD file |
| Added multiple files, want to unstage just one | git reset HEAD file-i-dont-want.js |
| Want to see what's staged before committing | git diff --staged |
| Unstage everything but keep the changes | git reset |
I messed up my WORKING DIRECTORY (unstaged changes)
| What happened | Fix it with |
|---|---|
| Edited a file and want to undo the edit | git checkout -- file |
| Want to discard all local changes | git checkout -- . |
| Want to remove untracked files | git clean -fd |
| Want to undo changes in specific files only | git checkout -- file1.js file2.js |
I messed up BRANCHES
| What happened | Fix it with |
|---|---|
| Merged by accident | git reset --hard ORIG_HEAD |
| Want to move a commit to another branch | git log (copy the hash)<br>Switch to the target branch<br>git cherry-pick <hash> |
| Branch name is wrong | git branch -m new-name |
| Deleted a branch by accident | git reflog (find the hash)<br>git checkout -b recovered-branch <hash> |
Emergency: I REALLY messed up
| What happened | Fix it with |
|---|---|
| Need to undo multiple commits | git reset --soft HEAD~3 (undoes the last 3 commits, keeps changes) |
Accidentally ran git reset --hard | git reflog → find the commit hash → git reset --hard <hash> |
| Want to revert a pushed commit safely | git revert <commit-hash> (creates a new commit that undoes it) |
One-Line Cheat Sheet
| Command | What it does | Safe? |
|---|---|---|
git status | Shows the current state of your repo | Safe |
git log --oneline | Shows commit history | Safe |
git diff | Shows unstaged changes | Safe |
git checkout -- file | Discards changes in a file | Use with caution |
git reset HEAD file | Unstages a file | Safe |
git reset --soft HEAD~1 | Undoes commit, keeps changes staged | Safe |
git reset --hard HEAD~1 | Deletes last commit and its changes | Destructive |
git commit --amend | Fixes the last commit | Use with caution |
git clean -fd | Deletes untracked files | Destructive |
Golden Rules for Undoing Git Mistakes
- Always run
git statusfirst — it tells you exactly what's going on. --hardis the nuclear option — it deletes things permanently.git reflogis your time machine — if you truly break something, it shows every action you've taken, so you can jump back.- When in doubt,
git stash— it temporarily saves your changes so you can experiment safely.
Pro tip: If you've already pushed to a remote, avoid
resetand userevertinstead:git revert <bad-commit-hash>This creates a new commit that undoes the bad one — safe for shared branches.
Frequently Asked Questions
How do I undo the last Git commit but keep my changes?
Run git reset --soft HEAD~1. This removes the commit but leaves your changes
staged, ready to re-commit.
What's the difference between git reset --soft and --hard?
--soft keeps your changes (staged), while --hard permanently deletes both
the commit and the changes. Use --soft when in doubt.
How do I recover a deleted branch or a lost commit?
Run git reflog to find the commit's hash, then
git checkout -b recovered-branch <hash> to restore it. Reflog records nearly
everything, so most "lost" work is recoverable.
How do I undo a git reset --hard?
Use git reflog to find the hash of the commit you were on before the reset,
then run git reset --hard <hash> to return to it.
How do I undo a commit that's already been pushed?
Use git revert <commit-hash>. It creates a new commit that reverses the bad
one, which is safe for branches other people share. Avoid reset on pushed
history.
How do I fix a typo in my last commit message?
Run git commit --amend -m "Correct message". If the commit is already pushed,
you'll need to force-push, so only amend unpushed commits when possible.