No tutorials. No theory. Just git commands that fix mistakes — when you're already panicking.
--force — but read the warning first.
git reset --soft HEAD~1
HEAD~1 with HEAD~N to undo N commits.git reset HEAD~1
--mixed is default).git reset --hard HEAD~1
git reflog them back).git reset
git reset --mixed HEAD.git restore --staged <file>
git reset HEAD <file>git restore <file>
git checkout -- <file>git revert <commit-hash>
--no-edit to skip the editor.reset --hard commits others have pulled. Always use revert for shared branches.
git reflog
git branch <branch-name> <commit-hash>
git branch {{branch-name}} $(git reflog | grep '{{branch-name}}' | tail -1 | awk '{print $1}')
git branch -m <new-name>
git branch -m <old-name> <new-name>
git push origin <branch-name>
reflog + git branch, just push it back.git merge --abort
git merge --strategy-option theirs
git merge --strategy-option ours
git checkout --theirs -- <file>
git add <file>
git checkout --ours -- <file>
git add <file>
git revert --mainline 1 -m 1 <merge-commit-hash>
-m 1 tells revert which parent to keep (usually the branch you were on).git reset --hard HEAD~1
revert above for pushed merges.git stash pop
git stash drop'd it:git stash list
git fsck --unreachable | grep commit | cut -d' ' -f3 | xargs git log --no-walk --oneline | head -10
git stash branch <new-branch> [stash@{N}]
git stash apply [stash@{N}]
pop, this keeps the stash in the list.git remote set-url origin <new-url>
git remote -vgit push --force-with-lease
--force. Aborts if the remote branch has new commits you haven't seen.git revert HEAD
git push origin <branch>
git reset --hard HEAD~1
git push --force-with-lease origin <branch>
git remote add origin <url>
git remote remove <name>
git reflog
reset --hard, branch -d, or amend.git blame <file>
git log --grep="<keyword>"
git log -S "<string>"
git log -- <file-path>
git log --oneline --graph --all --decorate
| Situation | Command | Warning |
|---|---|---|
| Undo last commit (keep staged) | git reset --soft HEAD~1 | Safe |
| Undo last commit (keep unstaged) | git reset HEAD~1 | Safe |
| Undo last commit (discard) | git reset --hard HEAD~1 | ⚠️ Destructive |
| Unstage all files | git reset | Safe |
| Unstage one file | git restore --staged <file> | Safe |
| Discard file changes | git restore <file> | ⚠️ Destructive |
| Revert a pushed commit | git revert HEAD | ✅ Safe for shared |
| Recover deleted branch | git reflog → git branch <b> <h> | Safe (90 day window) |
| Rename branch | git branch -m <new> | Safe |
| Abort merge | git merge --abort | Safe |
| Resolve with theirs | git merge -X theirs | Know what you're doing |
| Undo merge commit | git revert -m 1 <hash> | Safe |
| Recover dropped stash | git fsck --unreachable + reflog | Possible but messy |
| Stash to new branch | git stash branch <b> | Safe |
| Fix remote URL | git remote set-url origin <url> | Safe |
| Force push safely | git push --force-with-lease | ✅ Safer than --force |
| Undo push (safe) | git revert HEAD; git push | ✅ Safe |
| Find deleted commit | git reflog | Safe |
| Search commit messages | git log --grep="word" | Safe |
| Search by code change | git log -S "string" | Safe |
Git Emergency Room is $1. One coffee. One lifesaver.