Git Emergency
I want to undo my last commit
Severity Minor
What just happened
The commit exists locally (and maybe on the remote). The right command depends on whether it was pushed and whether you want to keep the changes.
Potential risks
- reset rewrites history: never on commits others have pulled.
Before you run anything
- Already pushed? Use git revert, the team-friendly undo.
- Golden rule: before any destructive command, create a safety branch with git branch backup/before-fix — it costs nothing.
Recovery steps
- 1
Only the message is wrong
Not pushed yet: rewrite the message.
Safe$ git commit --amend -m "feat: the right message" - 2
Undo the commit, keep the changes
The changes stay staged, ready to be committed again.
Caution$ git reset --soft HEAD~1 - 3
Already pushed: revert it
Adds a commit that cancels it, without rewriting history.
Safe$ git revert HEAD$ git push - 4
Throw the commit and its changes away
Gone from the branch (still in the reflog for a while).
Destructive$ git reset --hard HEAD~1
These procedures are provided for information only. Read each command and adapt it to your situation before running it. Disclaimer
Other disasters
🪓 I deleted my branch🚨 I pushed directly to main💥 I ran git reset --hard🧨 I overwrote a colleague's commits🔑 I pushed my .env file to GitHub🔀 I made a bad merge👻 I lost uncommitted changes🎯 I committed on the wrong branch🎈 I'm in “detached HEAD” state⚔️ I'm stuck in a merge conflict🚫 My push is rejected (non-fast-forward)🌀 My rebase went wrong🐘 GitHub rejects a file over 100 MB🥸 I committed with the wrong name or email🗑️ I deleted a file and committed it✏️ I amended a commit that was already pushed🧹 Too many messy commits to clean up📮 I pushed to the wrong branch🧭 I rebased onto the wrong branch🔁 I need back the changes I reverted📦 git stash pop gave me a conflict🙈 I committed a file that should be ignored🔠 Git doesn't see my file rename (upper/lower case)🧨 I ran git clean -fd by mistake🩻 My repository is corrupted🙃 I forgot to pull before pushing👯 Duplicated lines of code after a merge➖ I added a file I didn't want to (git add)✏️ I made a typo in a commit message✂️ My commit does too many things🧺 I lost a stash (drop, clear or pop gone wrong)🫥 I pulled and my code disappeared🍒 I cherry-picked the wrong commit☁️ I deleted a branch on GitHub / GitLab👥 I pushed with the wrong GitHub account🚚 The repository was renamed or moved🏷️ I tagged the wrong commit🌊 Git says every file is modified, but I changed nothing📦 I committed node_modules (or a build folder)📂 I ran git init in the wrong folder🧨 My pull request has conflicts⏳ My pull request is behind main📝 I have to apply review changes to my PR🧳 My PR shows commits that aren't mine🎯 I opened my PR against the wrong branch🚨 A merged PR broke main🚪 I closed my PR (or deleted its branch) by mistake🔴 The checks of my PR are failing🐘 My PR is too big to review🍴 My fork is behind the original repository