Reference

Branch cheat sheet

The commands from the course, grouped by what you are trying to do. Git 2.56 · gh 2.100.

GitHub flow: create a branch → make changes → create a pull request → address review comments → merge your pull request → delete your branch (GitHub Docs).

See where you are

CommandWhat it doesLesson
git statusCurrent branch, whether it is ahead of its upstream, uncommitted changes.2
git branchList local branches; * marks the one HEAD is on.2
git branch -vvEach branch's last commit, its upstream, and ahead/behind counts (docs).4
git branch -aLocal and remote-tracking branches (origin/…).4
git log --oneline --graph --allThe whole commit graph, one line per commit, with branch labels.2

Create & switch

CommandWhat it doesLesson
git switch -c NAMECreate a branch at the current commit and switch to it — the everyday command (docs).2
git switch NAMEMove HEAD to an existing branch; the working tree is updated to match.2
git switch -Jump back to the previous branch.2
git branch NAMECreate a branch but stay where you are.1
git checkout -b NAMEOlder equivalent of switch -c; you will see it in tutorials and agent output.2

Compare

CommandWhat it doesLesson
git diff main...featureThree-dot diff: only what feature changed since it left main — what a PR shows (docs).2
git log --oneline main..featureCommits on feature that are not on main.2
git merge-base main featurePrint the merge base: the commit where feature split from main (docs).10
git diff main..featureTwo-dot diff: how the two tips differ right now. Work that landed on main after the split shows reversed.10
git log --oneline HEAD..origin/mainAfter git fetch: commits on origin/main that your branch lacks. Any output means a stale branch.11
gh pr diff · --name-onlyThe current branch's PR diff, or just the changed file names.6

Merge & clean up

CommandWhat it doesLesson
git merge NAMEBring NAME into the current branch: fast-forward if possible, otherwise a merge commit (docs).3
git merge --no-ff NAMEAlways create a merge commit (what GitHub's "Create a merge commit" does).3
git merge --abortMid-conflict: give up and restore the pre-merge state.3
git branch -d NAMEDelete a branch — refused unless it is fully merged (safe).3
git branch -D NAMEForce-delete, merged or not — throws the work away.3
git push origin --delete NAMEDelete the branch on GitHub (removes the pointer only).4
git branch --delete-merged PATTERNNew in Git 2.56: delete local branches already merged into their upstream (docs).4

Conflict markers

<<<<<<< HEAD
df = df.dropna()            # your side (current branch)
=======
df = df.dropna(subset=cols) # their side (branch being merged)
>>>>>>> clean-data

Edit into the version you want, delete all three marker lines, then git add FILE and git commit (or git merge --continue) (docs).

Remotes

CommandWhat it doesLesson
git push -u origin NAMEPublish a new branch and set its upstream (docs).4
git pushPush the current branch to its upstream (after -u).4
git fetchDownload new commits and move origin/… pointers; never touches your files.4
git pullgit fetch then merge the upstream into the current branch.4

Pull requests with gh

CommandWhat it doesLesson
gh repo create NAME --public --clone --add-readmeNew GitHub repo with a first commit, cloned locally.2
gh pr create --fillOpen a PR from the current branch, title/body from commits. Add --draft, --base, --title, --body as needed (manual).4
gh pr view --webOpen the PR in your browser.4
gh pr checks --watchWatch the PR's status checks until they finish.5
gh pr merge --squash --delete-branchSquash merge and delete the branch locally and on GitHub. Alternatives: --merge, --rebase; --auto merges once requirements pass.4

Write Closes #12 in the PR body to close issue 12 on merge — only works when the PR targets the default branch (GitHub Docs).

Worktrees (parallel sessions)

CommandWhat it doesLesson
git worktree add ../proj-a -b feature-aNew folder with a new branch checked out, sharing the same repository (docs).7
git worktree add ../proj-baseline -b baseline $(git merge-base main feature)Worktree on a new branch at the merge base, for a baseline run of an eval.12
git worktree listMain worktree first, then linked ones with their branches.7
git worktree remove ../proj-aRemove a clean worktree (--force if it has changes).7
git worktree pruneClean up records of worktrees whose folders are gone.7
claude -w NAMEStart Claude Code in .claude/worktrees/NAME/ on new branch worktree-NAME (docs).7
claude --worktree NAME --resumeReturn to a kept worktree session.7

A branch can be checked out in only one worktree at a time.

Escape hatches

CommandWhat it doesLesson
git stash (-u for untracked)Shelve uncommitted changes to get a clean working tree (docs).2
git stash popRe-apply the latest stash and drop it from the list.2
git restore FILEDiscard uncommitted edits to FILE (docs).6
git restore --staged FILEUnstage FILE, keeping the edits.6