Key Points

Welcome and the Story


  • This workshop is about curating history: leaving commits that are easy to review, merge and understand
  • The same code can have a confusing history or a clear one, and the clear one is far easier to work with
  • Day 1 is about producing good history, and day 2 is about sharing and maintaining it
  • You’ll work in pairs, and your partner reviews your pull request

A Mental Model: Snapshots and Labels


  • A commit is a snapshot of the project plus a pointer to its parent. Diffs are worked out when needed
  • A commit’s hash depends on its contents and its parent, so changing one commit changes all those after it
  • You can’t edit a commit, only make a new one. The old one is still there
  • Branches and HEAD are just labels pointing at commits. Committing moves the label

Planning and Atomic Commits


  • Plan your commits before you start: a short list keeps each change small
  • An atomic commit does one logical thing and leaves the project working
  • A commit message has a short imperative subject, and a body that says why when it isn’t obvious
  • Atomic commits make review, revert, cherry-pick and bisect much easier

The Safety Net: Reflog


  • Git rarely deletes commits straight away. The reflog records where HEAD has been, so you can find them
  • Recover by copying the hash from git reflog and using git reset --hard <hash> or git branch <name> <hash>
  • The reflog is local and expires, and doesn’t cover uncommitted work
  • Commit before trying something risky

Shaping a Commit: Staging, Amend, Reset and Restore


  • The staging area lets you assemble a commit. Stage hunks or individual lines in VS Code (or with git add -p)
  • git commit --amend replaces the last commit, so only do it for commits you haven’t shared
  • git reset moves the branch label: --soft keeps changes staged, --mixed (default) keeps them unstaged, --hard discards them
  • git restore discards edits or unstages files without moving any label

Interactive Rebase: Rewriting a Messy Branch


  • git rebase -i <base> lets you edit every commit on a branch since <base>
  • The todo list is oldest first: change the command word to reword, drop, fixup or edit, and move lines to reorder
  • Rebase makes new commits. The old ones remain, so a backup branch or the reflog gets you back
  • Use edit and git reset HEAD^ to split a commit into several
  • git rebase --abort undoes a rebase that is going badly
  • Only rewrite history that nobody else has yet

The Daily Loop: Fixup and Autosquash


  • Plan a few commits, then commit small changes as you go
  • When you spot a mistake in an earlier commit, git commit --fixup <commit> leaves a note, without stopping your flow
  • Before sharing, git rebase -i --autosquash main folds each fixup into its target, and you can also reword, reorder, drop or edit commits until the history reads well
  • git rebase main --exec "pytest -q" checks every commit passes

Day 2 Recap and Warm-up


  • Day 1 was about producing good history: snapshots, atomic commits, rewriting and the daily loop
  • Day 2 is about sharing it and working alongside other people’s changes
  • Check your branch and your fork are in order before starting

Rebase vs Merge, and Resolving Conflicts


  • A fast-forward just moves a label, so main stays a straight line
  • Merge joins two tips with a merge commit. Rebase moves your branch’s base, and replays your commits as new ones
  • A conflict means both sides changed the same lines. Resolve it by editing the result to contain what you want. The VS Code merge editor makes this easy
  • In a rebase, “Current” is the branch you’re rebasing onto and “Incoming” is your commit
  • After a rebase, push with --force-with-lease. Never follow git’s hint to git pull
  • Only rewrite history that nobody else is depending on
  • For pull requests, “Rebase and merge” keeps your curated commits and a straight history

Tracking Down a Bug: Blame and Bisect


  • Blame shows which commit last changed each line (in VS Code: Git: Toggle Git Blame Editor Decoration). It shows only the last change, so it isn’t proof of the cause
  • git bisect binary-searches history for the first commit where a test fails. git bisect run <command> automates it. Always finish with git bisect reset
  • Fix bugs you find on a branch of their own, so the fix is a single idea that can be reviewed and reused

Pull Requests: Authoring and Reviewing


  • A pull request is a story. Review your own first: it should read well, each commit should pass, it should be up to date and contain only your change
  • Write a description that says what, why, and how to check it
  • Review one commit at a time. Be specific, ask questions, mark small things with nit:, suggest changes, and say what’s good
  • Respond to feedback with --fixup commits that you push as they are, and autosquash when it’s approved (say so first). If your team squash-merges, plain “address review” commits are fine
  • git diff @{u} HEAD printing nothing (or Compare with Remote in VS Code showing no files) proves that tidying the history didn’t change the code
  • Use Rebase and merge to keep your curated commits and a straight history
  • A pull request from a fork defaults to the original repository, so check the base repository is your own fork

Catching Problems Early: Shift Left and CI


  • “Shift left”: find problems as early, cheaply and quickly as you can. Push each check left until it becomes too slow or annoying to run there
  • Formatting and linting belong in your editor. Tests you run yourself before pushing, and CI runs them again for everyone, plus anything slower
  • Pre-commit hooks are an optional personal convenience, and can be skipped. CI is the shared gate that can’t
  • A GitHub Actions workflow is a YAML file in .github/workflows/: when to run, where, and a list of steps
  • Fix a failing check in the commit it belongs to (--fixup, then autosquash), so that every commit passes

Releases: Versions, Tags and Cherry-pick


  • Most projects follow a loop: develop on main, release with a version number and a tag, fix bugs on main, and release again
  • Semantic versioning (MAJOR.MINOR.PATCH) tells users what to expect from an upgrade
  • A tag is a label that never moves, unlike HEAD and branches. Use annotated tags (git tag -a) for releases, and push them explicitly
  • If users can upgrade, fix on main and release again. If they can’t, patch the old release on a release branch
  • git cherry-pick -x <commit> replays one commit’s change as a new commit, and atomic commits are what make it safe

Tooling and Wrap-up


  • Aliases and settings are personal choices: git’s defaults are fine, and you only need to change what’s bothering you
  • VS Code covers staging, amending, history, blame, conflicts, stash and cherry-pick. Fixup, interactive rebase, the reflog, bisect and range-diff need the terminal
  • Pick one habit to adopt first. Atomic commits and fixup with autosquash give the most for the least effort