← 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

CommandDescription
git initInitialize 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 --listShow all current configuration

Everyday Commits

CommandDescription
git statusShow 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 --amendFix up the last commit (edit the message or add files) — only before pushing
git diffShow unstaged changes; git diff --staged shows what's staged
git log --oneline -10Show the last 10 commits, one per line
git stash / git stash popShelve changes temporarily to do something else / restore the shelved changes

Branches

CommandDescription
git branchList 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 -aShow all branches, including remote ones

Syncing with Remotes

CommandDescription
git fetchOnly downloads remote updates into local refs without touching the working tree — the safest option
git pullFetch and merge (= fetch + merge) — the everyday update command
git pushPush 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 -vShow remote URLs — check this first when a push fails

Rollback and Undo (Rescue)

CommandDescription
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~1Undo the last commit, changes go back to the staging area — for "committed too soon"
git reset --hard HEAD~1Undo 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 reflogShow 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.

Keep looking things up

HTTP Status Codes →   Cron Expression Parser →