Git Rescue Cheat Sheet

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 happenedFix it with
Committed too early, want to undo but keep changesgit reset --soft HEAD~1
Committed to the wrong branch, want to move the commitgit reset --soft HEAD~1, then git stash and switch branches
Commit message has a typogit commit --amend -m "Correct message"
Forgot to add a file to the commitgit 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 happenedFix it with
Added a file by accidentgit reset HEAD file
Added multiple files, want to unstage just onegit reset HEAD file-i-dont-want.js
Want to see what's staged before committinggit diff --staged
Unstage everything but keep the changesgit reset

I messed up my WORKING DIRECTORY (unstaged changes)

What happenedFix it with
Edited a file and want to undo the editgit checkout -- file
Want to discard all local changesgit checkout -- .
Want to remove untracked filesgit clean -fd
Want to undo changes in specific files onlygit checkout -- file1.js file2.js

I messed up BRANCHES

What happenedFix it with
Merged by accidentgit reset --hard ORIG_HEAD
Want to move a commit to another branchgit log (copy the hash)<br>Switch to the target branch<br>git cherry-pick <hash>
Branch name is wronggit branch -m new-name
Deleted a branch by accidentgit reflog (find the hash)<br>git checkout -b recovered-branch <hash>

Emergency: I REALLY messed up

What happenedFix it with
Need to undo multiple commitsgit reset --soft HEAD~3 (undoes the last 3 commits, keeps changes)
Accidentally ran git reset --hardgit reflog → find the commit hash → git reset --hard <hash>
Want to revert a pushed commit safelygit revert <commit-hash> (creates a new commit that undoes it)

One-Line Cheat Sheet

CommandWhat it doesSafe?
git statusShows the current state of your repoSafe
git log --onelineShows commit historySafe
git diffShows unstaged changesSafe
git checkout -- fileDiscards changes in a fileUse with caution
git reset HEAD fileUnstages a fileSafe
git reset --soft HEAD~1Undoes commit, keeps changes stagedSafe
git reset --hard HEAD~1Deletes last commit and its changesDestructive
git commit --amendFixes the last commitUse with caution
git clean -fdDeletes untracked filesDestructive

Golden Rules for Undoing Git Mistakes

  1. Always run git status first — it tells you exactly what's going on.
  2. --hard is the nuclear option — it deletes things permanently.
  3. git reflog is your time machine — if you truly break something, it shows every action you've taken, so you can jump back.
  4. 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 reset and use revert instead:

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.

Last Updated on Jul 13, 2026