GitGroundworkGit
18 / 18 written chapters

G18 · Part 4 · Mastery

Cheat sheet

Every command worth knowing, grouped by the job you're doing, plus the setup to run once per machine.

8 min read1 diagram14 sections
14On this page
  1. One-time setup on a new machine
  2. Which undo do I need?
  3. Setup and config
  4. Inspect
  5. Stage and commit
  6. Branch
  7. Remote
  8. Merge and rebase
  9. Undo
  10. Investigate
  11. Stash and worktrees
  12. Tags and releases
  13. Newer commands worth knowing
  14. How to actually learn this
Fresher Mid Senior

One-time setup on a new machine

Worth doing properly, once. These settings remove most everyday friction: no more "set upstream" errors, clearer conflicts, remembered resolutions and pruned remote branches.

git config --global user.name "Your Name"
git config --global user.email "you@company.com"
git config --global init.defaultBranch main
git config --global pull.rebase true
git config --global push.autoSetupRemote true
git config --global push.followTags true
git config --global fetch.prune true
git config --global merge.conflictStyle zdiff3
git config --global rerere.enabled true
git config --global rebase.autoSquash true
git config --global rebase.updateRefs true
git config --global diff.algorithm histogram
git config --global branch.sort -committerdate
git config --global core.editor "code --wait"

git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.st "status -sb"
git config --global alias.last "log -1 --stat"
git config --global alias.unstage "restore --staged"
git config --global alias.please "push --force-with-lease --force-if-includes"

Check where any setting comes from with git config --show-origin --get pull.rebase, and list everything with git config --list --show-origin.

Which undo do I need?

Is the change committed? Is it staged? Has it been pushed? no yes yes no no yes restore --staged f unstage, keep the edits git restore f discards edits for good reset --soft HEAD~1 or --amend, or rebase -i git revert <sha> new commit, safe to push Lost something? git reflog lists every commit HEAD pointed to, kept for 30 to 90 days.
Two questions decide it. Uncommitted work is cheap to undo but easy to lose for good. Pushed commits are undone with a new commit, never by rewriting what others already have.

Setup and config

CommandDoes
git initStart a repository in the current folder
git clone <url>Copy a remote repository with its full history
git clone --filter=blob:none <url>Clone all history, fetch file contents on demand
git clone --depth 1 <url>Latest commit only, for CI
git config --global <key> <value>Set an option for every repository you use
git config --list --show-originEvery setting, and the file it came from
git config get / set / unset / listThe same jobs as the flag forms, as subcommands (Git 2.46 and later)
git help <command>The full manual page

Inspect

CommandDoes
git status -sbCompact state: branch, ahead or behind, changed files
git diffUnstaged changes
git diff --stagedExactly what the next commit will contain
git diff main...HEADWhat your branch changed since it left main
git log --oneline --graph --allThe whole commit graph, one line per commit
git log -p -- <file>Every change to one file, with diffs
git log main..featureCommits on feature that main doesn't have
git show <sha>One commit's message and diff
git show <sha>:path/fileA file as it was at that commit
git shortlog -snCommit counts per author

Stage and commit

CommandDoes
git add <file>Stage a file
git add -pStage hunk by hunk
git commit -m "..."Commit the staged changes
git commit -vWrite the message with the diff in view
git commit --amendReplace the last commit (unpushed only)
git commit --amend --no-editAdd staged changes to the last commit, keep the message
git commit --fixup=<sha>A fix to be squashed into an earlier commit
git commit -sAdd a Signed-off-by trailer
git commit -SSign the commit with your GPG or SSH key
git rm --cached <file>Stop tracking a file, keep it on disk
git mv <old> <new>Rename, including case-only renames

Branch

CommandDoes
git branch -vvLocal branches with their upstream and ahead or behind counts
git switch <name>Move to a branch
git switch -c <name>Create a branch and move to it
git switch -Back to the previous branch
git switch --detach <sha>Look at an old commit without a branch
git branch -m <new>Rename the current branch
git branch -d <name>Delete a merged branch
git branch -D <name>Delete even if unmerged
git branch --merged mainBranches already merged, safe to delete

Remote

CommandDoes
git remote -vList remotes and their URLs
git remote add upstream <url>Add a second remote, such as the original of a fork
git fetch --pruneDownload new commits, drop refs deleted on the remote
git pullFetch, then merge or rebase into the current branch
git pushSend the current branch to its upstream
git push -u origin <branch>First push, and set the upstream
git push --force-with-leaseForce-push without overwriting commits you haven't seen
git push origin --delete <branch>Delete a remote branch

Merge and rebase

CommandDoes
git merge <branch>Merge into the current branch, fast-forward if possible
git merge --no-ff <branch>Always record a merge commit
git merge --squash <branch>Stage the branch's changes as one commit
git merge --abortBack out of a conflicted merge
git rebase mainReplay your commits on top of main
git rebase -i <base>Squash, reword, reorder or drop commits
git rebase --onto <new> <old>Move a branch from one base to another
git rebase --continue / --abortCarry on after resolving, or give up and restore
git cherry-pick -x <sha>Copy one commit here, noting the original hash
git checkout --ours / --theirs <file>Take one side of a conflicted file

Undo

CommandDoes
git restore <file>Discard unstaged edits to a file
git restore --staged <file>Unstage, keep the edits
git restore --source=<sha> <file>Bring back a file as it was at a commit
git reset --soft HEAD~1Undo the last commit, keep its changes staged
git reset HEAD~1Undo the last commit, keep its changes unstaged
git reset --hard <sha>Move the branch and discard all uncommitted work
git revert <sha>New commit that undoes a pushed one
git revert -m 1 <merge>Undo a whole merge
git reflogEvery place HEAD has been: find "lost" commits
git reset --hard ORIG_HEADUndo the merge, rebase or reset you just did
git clean -ndPreview which untracked files clean -fd would delete

Investigate

CommandDoes
git blame -w -C <file>Who last changed each line, ignoring whitespace and moves
git blame -L 40,60 <file>Blame just those lines
git log -S "text"Commits that added or removed that string
git log -G "regex"Commits whose diff matches a pattern
git log -L :funcName:fileHistory of one function
git log --follow <file>A file's history through renames
git grep "text" <sha>Search the files of any commit
git bisect start, good, badBinary-search for the commit that broke something
git bisect run <cmd>Let a script mark each step
git merge-base main HEADWhere your branch left main

Stash and worktrees

CommandDoes
git stash -uPark all changes, including untracked files
git stash push -m "msg" <file>Stash specific files with a label
git stash listShow stashes
git stash popReapply the latest stash and drop it
git stash apply stash@{2}Reapply a stash and keep it
git stash branch <name>Turn a stash into a branch
git worktree add ../hotfix mainA second working folder on another branch, no stashing
git worktree list / remove <dir>Manage extra worktrees

Tags and releases

CommandDoes
git tag -a v1.2.0 -m "msg"Annotated tag, the kind to use for releases
git tag -s v1.2.0 -m "msg"Signed annotated tag
git tag -l "v1.*"List matching tags
git push origin v1.2.0Push one tag
git push --follow-tagsPush commits plus annotated tags that point at them
git describe --tags --alwaysLabel like v1.2.0-3-g8f2c9d1 for builds
git tag -v v1.2.0Verify a tag's signature
git tag -d v1.2.0 then git push origin --delete v1.2.0Delete a tag locally and on the remote
git log v1.1.0..v1.2.0 --onelineWhat changed between two releases

Newer commands worth knowing

CommandDoesSince
git switch, git restoreBranches and files as two separate commands2.23
git diff --merge-base mainYour work, including uncommitted edits, against where you left main2.30
git push --force-with-lease --force-if-includesForce-push that also refuses remote commits you never integrated2.30
git maintenance startSchedule background upkeep for a big repository2.31
git log --remerge-diffShow only what a person changed while resolving a merge2.36
git sparse-checkout set <dirs>Only these folders on disk (cone mode is the default)2.37
git rebase --update-refsMove every branch in a stack in one rebase2.38
git merge-tree --write-tree a bTest a merge without touching your files2.38
git worktree add --orphan -b <name> <dir>A new worktree on a branch with no history2.42
git replay --onto <base> <range>Rebase without a working tree, for servers and tools (experimental)2.44
git refs migrate --ref-format=reftableMove an existing repository to the reftable backend2.46
git clone --revision=<rev> --depth 1Exactly one commit, for a reproducible build2.49
git backfillFill in a blobless clone's history in batches (experimental)2.49

How to actually learn this

  1. Make a throwaway repository today. Break it on purpose: hard reset too far, force-push over yourself, create a conflict. Recover each time. Confidence comes from having already broken things somewhere it didn't matter.
  2. Run git lg after every operation for a week. Watching the graph move is how the pointer model stops being abstract.
  3. Read your own diff before every commit. It's the highest-value habit on this page.
  4. When you're stuck, ask "where is the pointer, and where do I want it?" That question answers most Git problems.

Finished reading?

0 of 18 chapters read in this series