← Back to Toolbox
Git Cheat Sheet
Updated October 2026 · Grouped by scenario — no need to memorize, just look it up when you need it
Setup and Configuration
| Command | Description |
git init | Initialize a repository in the current directory |
git clone <repo-url> | Clone a remote repository to your machine |
git config --global user.name "Your Name" | Set your commit author name (global, only once) |
git config --global user.email "[email protected]" | Set your commit email — it must match your code-hosting account |
git config --list | Show all current configuration |
Everyday Commits
| Command | Description |
git status | Show working-tree status — the first command to run when you're lost |
git add <file> / git add . | Stage changes; . stages everything |
git commit -m "message" | Commit the staged changes |
git commit --amend | Fix up the last commit (edit the message or add files) — only before pushing |
git diff | Show unstaged changes; git diff --staged shows what's staged |
git log --oneline -10 | Show the last 10 commits, one per line |
git stash / git stash pop | Shelve changes temporarily to do something else / restore the shelved changes |
Branches
| Command | Description |
git branch | List local branches; * marks the current one |
git switch -c <new-branch> | Create and switch to a new branch (old spelling: checkout -b) |
git switch <branch> | Switch branches |
git merge <branch> | Merge the given branch into the current one |
git branch -d <branch> | Delete a local branch that has been merged |
git branch -a | Show all branches, including remote ones |
Syncing with Remotes
| Command | Description |
git fetch | Only downloads remote updates into local refs without touching the working tree — the safest option |
git pull | Fetch and merge (= fetch + merge) — the everyday update command |
git push | Push the current branch to the remote |
git push -u origin <branch> | Push a new branch for the first time and set its upstream; after that plain git push is enough |
git remote -v | Show remote URLs — check this first when a push fails |
Rollback and Undo (Rescue)
| Command | Description |
git restore <file> | Discard unstaged changes in a working-tree file (unrecoverable — think first) |
git restore --staged <file> | Unstage a file while keeping the changes themselves |
git reset --soft HEAD~1 | Undo the last commit, changes go back to the staging area — for "committed too soon" |
git reset --hard HEAD~1 | Undo the last commit and discard its changes — dangerous, be sure before you hit Enter |
git revert <commit-id> | Create a reverse commit that cancels a bad one — use this for code already pushed; it doesn't rewrite history |
git reflog | Show every HEAD movement — after a bad reset, this is how you find the commit id and save yourself |
Watch Out For
1. Never rewrite history with reset on commits you've already pushed — use revert, or your collaborators' pulls will conflict into oblivion.
2. Changes discarded by --hard and restore have no recycle bin. Run git stash or copy the files first.
3. Deleted something by accident? Don't panic: as long as it was committed, find its id in git reflog and git reset --hard <id> brings it back.