Git
Daily git plumbing — the moves I do without thinking.
Official docsStatus & inspect
git statusWhat's staged, unstaged, untracked, and which branch you're on.
git status -sbShort + branch summary. Easier to grep in scripts.
git diffUnstaged changes vs. the index.
git diff --stagedWhat's about to be committed.
git log --oneline --graph --decorate -20Compact branch graph for the last 20 commits.
git show <sha>Full commit message + diff for one commit.
git blame <file>Last commit that touched each line.
Stage & commit
git add <path>Stage a file or directory. -A stages everything.
git add -pInteractively stage hunks — invaluable for keeping commits focused.
git commit -m 'message'Commit the index with a one-line message.
git commit --amendReplace the last commit (message + tree). Don't do this after pushing without --force.
git restore --staged <file>Unstage without losing the change.
git restore <file>Throw away unstaged changes in a file.
Branches
git switch -c feature/xCreate + check out a new branch from HEAD. Modern alternative to `checkout -b`.
git switch masterSwitch to an existing branch.
git branch -d feature/xDelete a local branch (-D to force when not merged).
git branch -vvList branches with their tracking remotes + ahead/behind counts.
git merge --no-ff feature/xMerge with a real merge commit so the branch stays visible in history.
Remotes & sync
git fetchUpdate remote-tracking refs. No tree/working-dir changes.
git fetch --pruneAlso delete remote-tracking branches that no longer exist on the server.
git pull --rebaseFetch + rebase your local commits on top of the new tip. Default this in ~/.gitconfig.
git push -u origin HEADPush the current branch and set upstream tracking in one go.
git push --force-with-leaseSafer force-push: refuses to clobber commits you haven't seen.
Rebase, reset & rescue
git rebase -i HEAD~5Edit/squash/reorder the last 5 commits. Don't rebase commits that are already shared.
git rebase masterMove the current branch to start from the tip of master.
git reset --soft HEAD~1Undo the last commit but keep its changes staged.
git reset --hard origin/masterMake the working tree exactly match origin/master. Destructive — careful.
git reflogLocal history of every HEAD move. Lifesaver for un-deleting commits.
git stashSet aside uncommitted changes. `git stash pop` brings them back.
git cherry-pick <sha>Re-apply one commit on top of the current branch.