git
Git Cheat Sheet: The Commands You Actually Use (with the Gotchas)
A practical Git cheat sheet grouped by what you are trying to do — commit, branch, undo, stash, sync, and recover — with the gotchas that bite people. Copy-paste ready.
Most Git cheat sheets dump 200 commands on you alphabetically. This one is grouped by what you’re actually trying to do, with the handful of commands you reach for daily and the gotchas that cause the “wait, what did I just do” moments.
One-time setup
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
git config --global pull.rebase false # merge on pull (the safe default)
Start a repo and ignore the right files from day one (build output, secrets, node_modules):
git init
Generate a tailored
.gitignorefor your stack with our .gitignore generator — committing junk is much harder to undo than preventing it.
The everyday loop
| Command | What it does |
|---|---|
git status | What’s staged, modified, untracked |
git add <file> / git add -p | Stage a file / stage hunks interactively |
git commit -m "msg" | Commit staged changes |
git commit -am "msg" | Stage tracked changes and commit in one step |
git diff / git diff --staged | Unstaged changes / staged changes |
git log --oneline --graph --all | Compact, visual history |
Branching
git switch -c feature/login # create + switch (modern)
git switch main # switch to an existing branch
git branch -d feature/login # delete a merged branch
git branch -D feature/login # force-delete an unmerged branch
git branch -m old-name new-name # rename
git switch and git restore are the modern, less-overloaded replacements for git checkout. checkout still works, but it does three unrelated jobs (switch branches, restore files, detach HEAD), which is exactly why it’s confusing.
Undo & fix — the most-searched section
| You want to… | Command | Safe? |
|---|---|---|
| Edit the last commit message | git commit --amend | ✅ if not pushed |
| Add a forgotten file to the last commit | git add f && git commit --amend --no-edit | ✅ if not pushed |
| Unstage a file (keep changes) | git restore --staged <file> | ✅ |
| Discard changes to a file | git restore <file> | ⚠️ destroys edits |
| Undo last commit, keep changes staged | git reset --soft HEAD~1 | ✅ |
| Undo last commit, keep changes unstaged | git reset HEAD~1 | ✅ |
| Undo last commit, throw away changes | git reset --hard HEAD~1 | ⚠️ destructive |
| Revert a commit that’s already pushed | git revert <sha> | ✅ safe for shared history |
The golden rule: reset rewrites history (use only on commits you haven’t pushed); revert makes a new commit that undoes an old one (safe to share). Never reset --hard or force-push a branch other people are building on.
Stash — park work without committing
git stash push -m "wip: half-done nav"
git stash list
git stash pop # apply most recent and drop it
git stash apply stash@{1} # apply a specific one, keep it
git stash -u # include untracked files
Sync with a remote
git remote -v
git fetch origin # download refs, change nothing local
git pull # fetch + merge into current branch
git push -u origin feature/x # push and set upstream (first time)
git push # subsequent pushes
fetch is always safe — it never touches your working tree. pull is fetch + merge, so it can create conflicts. When in doubt, fetch first and look before you merge.
Inspecting & finding
git log -p <file> # history of one file, with diffs
git log -S "functionName" # commits that added/removed that string
git blame <file> # who last touched each line
git show <sha> # a single commit's diff
git diff main...feature # what feature adds vs the fork point
Rebase (cleanly, without fear)
Rebase replays your commits on top of another branch for a linear history:
git switch feature
git rebase main # move feature's commits on top of latest main
git rebase -i HEAD~3 # squash/reword/reorder the last 3 commits
Gotcha: only rebase commits you haven’t shared. Rebasing rewrites SHAs, so rebasing pushed commits forces everyone else to deal with diverged history. If you must push a rebased branch, use git push --force-with-lease (safer than --force — it refuses if someone else pushed in the meantime).
The “I broke everything” recovery kit
git reflog # a log of everywhere HEAD has been
git reset --hard HEAD@{2} # jump back to a previous state from reflog
git reflog is the single most underrated command. Even after a bad reset --hard or a deleted branch, the commits usually still exist — reflog finds them so you can recover. Git rarely truly loses committed work.
Quick reference: the daily ten
git status
git add -p
git commit -m "..."
git switch -c branch
git pull
git push
git log --oneline --graph
git restore --staged <file>
git stash
git reflog
Learn these ten cold and you’ve covered ~90% of real Git use. Reach for the rest when you need them — and bookmark this page for when you do.
Setting up a new project? Start it right with a stack-specific .gitignore so secrets and build artifacts never enter history in the first place.