You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A high-density reference sheet of essential Git and GitHub commands, organized by category, followed by the 🚨 Git Emergency Room triage guide for instant recovery from mistakes.
Pushes branch to remote and sets upstream tracking
git push -u origin main
git push
Pushes commits to the tracked upstream branch
Used after -u has been configured
git pull
Fetches remote changes AND merges them into active branch
Shortcut for fetch + merge
git fetch
Downloads objects and refs from remote without merging
Inspect remote work safely
git push origin --delete <branch>
Deletes a branch on GitHub remote
git push origin --delete feat/legacy
git fetch --prune
Removes stale remote tracking branches
Cleans up branches deleted on GitHub
🔍 5. Inspecting, Diffs & History
Command
Description
Example / Note
git diff
Shows unstaged changes in working directory
Compares working tree vs staging area
git diff --staged
Shows changes that are staged for the next commit
Compares staging area vs last commit
git diff <b1>..<b2>
Shows differences between two branches
git diff main..feature
git log
Displays full commit history
Standard multi-line log
git log --oneline
Displays compact one-line-per-commit history
Quick history scan
git log --oneline --graph --all
Visual ASCII branch and merge tree
High-level repository topology
git show <commit>
Shows details, metadata, and diff of a specific commit
git show 4a8b1c2
git blame <file>
Shows line-by-line author and commit history of a file
Great for finding who wrote a specific line
🎒 6. Stashing (Temporary Shelving)
Command
Description
Example / Note
git stash / git stash push -m "<msg>"
Saves dirty working changes to temporary stash
git stash push -m "WIP: auth"
git stash list
Lists all saved stashes
Shows stash@{0}, stash@{1}, etc.
git stash pop
Applies the latest stash AND removes it from list
Restores changes back to working tree
git stash apply
Applies the latest stash BUT keeps it in the stash list
Useful to apply stash on multiple branches
git stash -u
Stashes tracked AND untracked (new) files
Leaves directory completely clean
git stash drop stash@{0}
Deletes a specific stash from the list
Discards stashed changes
git stash clear
Permanently deletes all stashes
Empties the entire stash stack
⏪ 7. Undoing & Rewriting History
Command
Description
Example / Note
git restore <file>
Discards uncommitted changes in working directory
⚠️ Reverts file to last commit
git restore --staged <file>
Unstages a file while preserving your local edits
Moves file out of staging area
git revert <commit>
Creates a NEW commit that reverses specified commit
Safe for public branches
git reset --soft HEAD~1
Undoes last commit, keeps changes staged
Perfect for amending or reorganizing commits
git reset --mixed HEAD~1
Undoes last commit, keeps changes unstaged
Default reset behavior
git reset --hard HEAD~1
Undoes last commit and DESTROYS all local edits
⚠️ Irreversible data loss risk
git rebase <base-branch>
Replays current branch commits on top of base branch
Creates a clean linear history
git rebase -i HEAD~<N>
Interactive rebase for the last N commits
Squash, reword, edit, or drop commits
git cherry-pick <commit>
Applies specific commit from another branch to active branch
git cherry-pick 7a8b9c
🛠️ 8. Advanced Debugging & Recovery
Command
Description
Example / Note
git reflog
Logs every single movement of HEAD (commits, checkouts, resets)
The ultimate emergency safety net
git bisect start
Initiates binary search mode to locate bug introduction
git bisect start
git bisect bad
Marks current commit as buggy / broken
git bisect bad
git bisect good <commit>
Marks a past commit as working / clean
git bisect good v1.0.0
git bisect run <cmd>
Automatically tests commits using an automated script
git bisect run npm test
git bisect reset
Exits bisect mode and returns to starting branch
Cleans up bisect state
🚨 The Git Emergency Room (What to Do When Things Go Wrong)
The Scenario / Mistake
The Immediate Solution
"I accidentally committed directly to main instead of a feature branch!"
1. Create feature branch where you are: git branch feat/my-work 2. Wind main back 1 commit: git reset --hard HEAD~1 3. Switch to your feature branch: git switch feat/my-work
"I typed the wrong commit message on my last commit."