Git Rescue

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 wrong branch, want to move 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 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 onegit reset HEAD file-i-dont-want.js
Want to see what's staged before committinggit diff --staged
Unstage everything but keep changesgit reset

I messed up WORKING DIRECTORY (unstaged changes)

What happenedFix it with
Edited a file and want to undogit checkout -- file
Want to discard all local changesgit checkout -- .
Want to remove untracked filesgit clean -fd
Want to undo changes in multiple specific filesgit 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 hash)<br>Switch to target branch<br>git cherry-pick <hash>
Branch name is wronggit branch -m new-name
Deleted a branch by accidentgit reflog (find 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 (undo last 3 commits, keep changes)
Accidentally did git reset --hardgit reflog → find the commit hash → git reset --hard <hash>
Want to revert a pushed commit safelygit revert <commit-hash> (creates new commit that undoes it)

One-Line Cheatsheet

CommandWhat it doesSafe?
git statusShows what's happening
git log --onelineShows commit history
git diffShows unstaged changes
git checkout -- fileDiscard changes in file⚠️
git reset HEAD fileUnstage file
git reset --soft HEAD~1Undo commit, keep changes staged
git reset --hard HEAD~1Delete last commit and changes
git commit --amendFix last commit⚠️
git clean -fdDelete untracked files

Golden Rules

  1. Always run git status first - it tells you what's going on
  2. --hard is the nuclear option - it deletes things permanently
  3. reflog is your time machine - if you really screw up, git reflog shows EVERYTHING you've done
  4. When in doubt, git stash - temporarily saves your changes so you can try things safely

Pro tip: If you've already pushed to 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!

Last Updated on Mar 8, 2026