🏥 Git Emergency Room

CHEATSHEET v1.0

Broke your repo? Here's the fix.

No tutorials. No theory. Just git commands that fix mistakes — when you're already panicking.

🔴 Triage: If you haven't pushed yet, relax — almost everything is recoverable locally. Already pushed? Same commands work with --force — but read the warning first.
1 UNDO / ROLLBACK
Undo last commit (keep changes staged)
git reset --soft HEAD~1
Changes stay in staging. Swap HEAD~1 with HEAD~N to undo N commits.
Undo last commit (keep changes unstaged)
git reset HEAD~1
Files go back to working directory, not staged (--mixed is default).
Undo last commit (discard changes entirely)
git reset --hard HEAD~1
⚠️ Destructive. Those changes are gone (unless you git reflog them back).
Undo staged changes (unstage everything)
git reset
Unstages all files. Same as git reset --mixed HEAD.
Unstage a specific file
git restore --staged <file>
New-school way. Old-school: git reset HEAD <file>
Undo unstaged changes in a file
git restore <file>
Discards working-directory changes. Old-school: git checkout -- <file>
Revert a pushed commit (safe undo)
git revert <commit-hash>
✅ Safe for pushed commits. Creates a new commit that undoes the target. Use --no-edit to skip the editor.
Revert vs Reset
reset moves the branch pointer (rewrites history). revert creates a new commit that undoes changes. Never reset --hard commits others have pulled. Always use revert for shared branches.
2 BRANCH RECOVERY
Recover a deleted branch
git reflog
Find the commit hash of the last commit on the deleted branch, then:
git branch <branch-name> <commit-hash>
Recover from reflog (one-liner)
git branch {{branch-name}} $(git reflog | grep '{{branch-name}}' | tail -1 | awk '{print $1}')
Grep the reflog for the branch name, grab the hash, recreate it.
Rename current branch
git branch -m <new-name>
Rename any branch
git branch -m <old-name> <new-name>
Push a deleted branch back to remote
git push origin <branch-name>
After recovering locally with reflog + git branch, just push it back.
3 MERGE CONFLICTS
Abort a merge (go back to before)
git merge --abort
Stops the merge, resets to pre-merge state. Works even if files are a mess.
Resolve ALL conflicts with "theirs"
git merge --strategy-option theirs
For when you know the incoming branch is always right. Use on merge start, not mid-conflict.
Resolve ALL conflicts with "ours"
git merge --strategy-option ours
Resolve a conflict using theirs (mid-conflict)
git checkout --theirs -- <file>
git add <file>
Takes their version of one conflicted file.
Resolve a conflict using ours (mid-conflict)
git checkout --ours -- <file>
git add <file>
Undo a merge commit (already committed)
git revert --mainline 1 -m 1 <merge-commit-hash>
-m 1 tells revert which parent to keep (usually the branch you were on).
Undo a merge (already committed, alternative)
git reset --hard HEAD~1
⚠️ Only if you haven't pushed the merge commit. See revert above for pushed merges.
4 STASH
Recover a dropped stash
git stash pop
If you already git stash drop'd it:
git stash list
If dropped, look up the commit in reflog:
git fsck --unreachable | grep commit | cut -d' ' -f3 | xargs git log --no-walk --oneline | head -10
Apply stash to a different branch
git stash branch <new-branch> [stash@{N}]
Creates a new branch from where the stash was made, applies the stash, then drops it.
Apply stash without dropping
git stash apply [stash@{N}]
Unlike pop, this keeps the stash in the list.
5 REMOTE
Fix wrong remote URL
git remote set-url origin <new-url>
Check current: git remote -v
Force push safely (prevent overwriting others)
git push --force-with-lease
✅ Safer than --force. Aborts if the remote branch has new commits you haven't seen.
Undo a push (revert + push)
git revert HEAD
git push origin <branch>
Creates a revert commit and pushes it. No history rewrite needed.
Undo a push (hard reset + force push)
git reset --hard HEAD~1
git push --force-with-lease origin <branch>
⚠️ Rewrites history. Only safe on solo branches or with team consent.
Add a remote
git remote add origin <url>
Remove a remote
git remote remove <name>
6 LOG / HISTORY
Find a deleted commit (reflog)
git reflog
Shows every commit HEAD pointed to. Even after reset --hard, branch -d, or amend.
Find who changed what in a file
git blame <file>
Every line annotated with commit hash, author, and date.
Search commit messages
git log --grep="<keyword>"
Search commits that added/removed a string
git log -S "<string>"
The pickaxe option. Finds commits that changed the number of occurrences of a string.
Search commits that changed a file
git log -- <file-path>
One-line log with graph
git log --oneline --graph --all --decorate
The "I have no idea what's going on" view.
7 CHEAT SHEET — PRINTABLE
Quick reference. Print this page — the table fits one page.
Situation Command Warning
Undo last commit (keep staged)git reset --soft HEAD~1Safe
Undo last commit (keep unstaged)git reset HEAD~1Safe
Undo last commit (discard)git reset --hard HEAD~1⚠️ Destructive
Unstage all filesgit resetSafe
Unstage one filegit restore --staged <file>Safe
Discard file changesgit restore <file>⚠️ Destructive
Revert a pushed commitgit revert HEAD✅ Safe for shared
Recover deleted branchgit refloggit branch <b> <h>Safe (90 day window)
Rename branchgit branch -m <new>Safe
Abort mergegit merge --abortSafe
Resolve with theirsgit merge -X theirsKnow what you're doing
Undo merge commitgit revert -m 1 <hash>Safe
Recover dropped stashgit fsck --unreachable + reflogPossible but messy
Stash to new branchgit stash branch <b>Safe
Fix remote URLgit remote set-url origin <url>Safe
Force push safelygit push --force-with-lease✅ Safer than --force
Undo push (safe)git revert HEAD; git push✅ Safe
Find deleted commitgit reflogSafe
Search commit messagesgit log --grep="word"Safe
Search by code changegit log -S "string"Safe

💊 This saved you, right?

Git Emergency Room is $1. One coffee. One lifesaver.

0x0D42B32D6E21F496082104BFC8d7213081474577
ETH / USDT / USDC — any EVM chain