Sub topic 13
Habits that keep stacks manageable
This chapter assumes you can run git rebase --update-refs, push with --force-with-lease, and restack with --onto after a squash merge. It collects the habits that keep those commands cheap to run. A stack that is small, ordered, and restacked often rarely needs the recovery steps from earlier chapters.
Keep the stack short
Every branch in a stack adds rebase work. A conflict in the bottom branch can repeat in each branch above it. A stack of three or four branches is easy to reason about. A stack of eight usually means the work should have been split differently, or that early branches should have merged already.
If a stack keeps growing, stop adding branches. Get the bottom ones reviewed and merged first.
Give each branch one reviewable purpose
Each branch should make sense as a pull request on its own. A reviewer should be able to say what the branch does in one sentence. Good splits follow the dependency order of the work:
- Add the data structure or interface.
- Use it in one place.
- Add the user-facing change.
- Add documentation.
Avoid a branch that only exists to hold "fixes for the previous branch". Put those fixes into the branch they belong to.
Name branches so the order is visible
A shared prefix and a number make the stack readable in git branch output and in the pull request list:
git switch -c parser/1-skeleton main
# ...commit work...
git switch -c parser/2-tests
# ...commit work...
git switch -c parser/3-cli-flag
# ...commit work...
git branch --list 'parser/*'
git log --oneline --decorate --graph main..parser/3-cli-flagThe log command doubles as a health check. All three branch names should appear on a single straight line of commits. If the graph forks, a branch has been left behind.
Fix the right branch with fixup commits
When review feedback applies to a lower branch, do not commit it on the top branch. Make a fixup commit that targets the original commit, then let the rebase fold it in and move every branch:
git add -p
git commit --fixup <commit-id-of-the-original-change>
git rebase -i --autosquash --update-refs origin/mainThe fix lands in the correct branch, and the branches above it are updated in the same rebase. Because the todo list is open, you can also confirm that each update-ref line is still in the right place.
Restack on a routine
Small, frequent restacks produce small conflicts. Large, rare ones produce hard conflicts. Turn on rebase.updateRefs once, then repeat the same loop:
git config --global rebase.updateRefs true
git fetch origin
git switch parser/3-cli-flag
git rebase origin/main
git push --force-with-lease origin parser/1-skeleton parser/2-tests parser/3-cli-flagMerge from the bottom, then restack immediately
Pull requests merge in stack order. After the bottom branch is squash-merged, run git rebase --update-refs --onto origin/main <old-bottom-branch> from the top branch. Do this before anything else, so the next pull request shows only its own commits. Then change the base of the next pull request to main if your hosting service has not already done it. Delete the merged local branch once the restack succeeds.
Know what the tooling will not do
Remember the limits from earlier chapters:
--update-refsskips a branch that is checked out in another worktree. Checkgit worktree listbefore a big restack.- Every rebased branch needs a push. Pushing only the top branch leaves the open pull requests stale.
- Before a risky restack, note each branch tip with
git branch -vv. The reflog can recover them, but a written list is quicker to read.
Exercise
You have a stack of three branches. A reviewer asks for a change in the middle branch. Which commands apply the change so that the top branch also contains it, and why is git commit on the top branch the wrong choice?
Show answer
Stage the change, run git commit --fixup <commit-id> for the commit it belongs to, then run git rebase -i --autosquash --update-refs origin/main (or your stack's base). The fixup is folded into the middle branch's commit, and --update-refs moves every branch tip. A plain commit on the top branch would put the fix in the wrong pull request, and the middle branch would not contain it.
Exercise
Name two habits that reduce the size of conflicts during a restack.
Show answer
Restack often (fetch and rebase on a routine instead of rarely), and keep the stack short by merging the bottom branches as soon as they are approved. Both limit how far the stack drifts from main.