Sub topic 13
Habits that keep stacks manageable
This chapter assumes you can run git rebase --update-refs, force-push with --force-with-lease, and restack with --onto after a squash merge. The earlier chapters covered those commands. This one covers the habits that keep you from needing them in awkward situations.
A stack is cheap to create and expensive to neglect. Most stack problems come from a few causes: the stack grows too deep, branches mix unrelated work, or the stack sits unrebased for days. The habits below target those causes.
Keep each branch to one reviewable idea
A branch in a stack is a pull request waiting to happen. Size it for the reviewer, not for yourself. Use these checks before you start the next branch:
- Can you describe the branch in one sentence without using "and"?
- Could a reviewer approve it without reading the branches above it?
- Does the build pass and do the tests run at the tip of this branch alone?
The last check matters because the bottom branch will be merged before the others. If feature-1 only compiles when feature-2 is present, the merge puts a broken main in front of every user.
Good boundaries tend to follow the shape of the change:
| Layer | Example branch contents |
|---|---|
| Preparation | Rename a function, move a file, no behavior change |
| Foundation | New module with its own tests, not yet used |
| Wiring | Connect the module to existing code |
| Surface | CLI flag, UI change, documentation |
Putting pure refactors in the lowest branch is the most useful rule. Refactor diffs are large but easy to review, and they are the most likely to conflict with other people's work. Landing them first shrinks the conflict window.
Keep the stack shallow
Every extra layer multiplies restack work. A change in feature-1 forces a rebase of every branch above it, and a conflict can appear at any layer. A practical limit is three or four branches. If you want a fifth, stop and ask whether the lower branches can be merged first.
Review latency is the usual reason stacks grow. You finish branch 3 while branch 1 still waits for review, so you start branch 4. Instead, consider:
- Ask the reviewer for branch 1 directly.
- Work on something unrelated while you wait.
- Only extend the stack when the lower branches are close to approval.
A deep stack is a queue of unreviewed work, and unreviewed work loses value as main moves.
Name branches so the order is visible
Branch names should show position. Plain names like feature-1 work in tutorials, but real names should carry a shared prefix and a number:
git switch -c parser/1-skeleton main
git switch -c parser/2-tests
git switch -c parser/3-cli-wiringA shared prefix lets you list the whole stack with one pattern, and the numbers make the order obvious in git branch output:
git branch --list 'parser/*'
git for-each-ref --format='%(refname:short) %(objectname:short) %(subject)' refs/heads/parser/If the order changes later, renumbering is cosmetic and can wait. The branch relationships live in the commit graph, not in the names.
Restack early and often
A restack is a small job when main has moved a little and a large job when it has moved a lot. Run it at the start of each work session and before you ask for review:
git fetch origin
git switch parser/3-cli-wiring
git rebase --update-refs origin/mainWith rebase.updateRefs enabled, the last command needs no flag. Small, frequent rebases give you small conflicts, and you resolve them while you still remember what the code was meant to do.
Check the stack's health before you push
Two mistakes are easy to make and hard to notice. One is a lower branch that was not moved (for example, it was checked out in another worktree). The other is a rebase that changed more than you intended. Check for both before pushing.
First, verify that each branch still contains the one below it. This script walks the stack from the bottom and reports any branch that has fallen off its parent:
#!/usr/bin/env bash
set -euo pipefail
prev=origin/main
for branch in parser/1-skeleton parser/2-tests parser/3-cli-wiring; do
if git merge-base --is-ancestor "$prev" "$branch"; then
echo "ok $branch contains $prev"
else
echo "STALE $branch does not contain $prev"
fi
prev=$branch
donegit merge-base --is-ancestor A B exits with status 0 when commit A is reachable from B. A stale branch is one whose parent has been rewritten but which still points at the old commits.
Second, compare the stack before and after the rebase with git range-diff. It pairs up the old and new commits and shows only what changed between them:
git range-diff origin/main@{1}..parser/3-cli-wiring@{1} origin/main..parser/3-cli-wiringHere @{1} means "the previous position of this ref" from its reflog. origin/main@{1} is meaningful only if your last git fetch actually moved origin/main. If a commit shows as changed when you expected it to be identical, you resolved a conflict differently than you remember, and you can look at it before anyone else does.
The following diagram shows the loop these habits form.
Fold review feedback into the right branch
Review comments on a lower branch are the main reason you rewrite a stack. There are two clean ways to handle them.
The first is to switch to the branch, commit, and restack what is above it:
git switch parser/1-skeleton
# ...edit...
git commit -a -m "Handle empty input in parser skeleton"
git switch parser/3-cli-wiring
git rebase --update-refs parser/1-skeletonThis works but leaves a "fix" commit in the branch history. If you want the history clean, use a fixup commit instead. You commit once at the tip, aim it at the commit it belongs to, and let the rebase move it down:
git commit --fixup 1a2b3c4
git rebase -i --autosquash --update-refs origin/main--autosquash moves the fixup commit directly after its target and marks it fixup. Because --update-refs is also set, the update-ref lines stay in the right places and the branches follow. Review the todo list before you save it, as described in the chapter on update-ref lines. If you do this often, set rebase.autoSquash to true so that you do not need the flag.
The fixup approach changes a commit in a lower branch, so every branch from that point up gets new commit IDs. Push all of them.
Land from the bottom, and only from the bottom
The merge order is fixed: the bottom branch goes first. Merging a middle branch into main pulls in everything below it, and the lower pull request becomes empty or confusing. Habits that support this:
- Open each pull request against the branch below it, not against
main, so the diff shows only that branch's own commits. - After the bottom branch is merged, retarget the next pull request to
main. Many hosting platforms do this automatically when the base branch is deleted, but check, because a wrong base can show a diff of hundreds of unrelated commits. - Restack immediately after a squash merge, using the
--ontoform from the previous chapter, before you touch any other branch. - Do not merge
maininto a stack branch. A merge commit in the middle makes later--update-refsrebases flatten or replay history in ways that surprise you. Rebase instead.
Clean up after each landing
Dead branches clutter the stack and tempt you to push to the wrong one. After the bottom branch lands:
git fetch --prune origin
git branch -D parser/1-skeletonYou need -D because Git cannot tell that a squash-merged branch is merged. Its commits differ from the squash commit, so git branch -d reports it as unmerged. Delete it only after you have confirmed on the remote that the squash commit is in main. The reflog keeps the old tip for a while, so a mistake here is recoverable, as covered in the reflog chapter.
Then confirm that the remaining branches now start at origin/main:
git log --oneline --decorate --graph origin/main..parser/3-cli-wiringYou should see only the commits of the remaining branches, with their branch names beside them.
Know when to stop stacking
Stacking is a tool for changes that depend on each other. It is the wrong tool when:
- The pieces are independent. Make separate branches from
mainand avoid the restack cost. - The lower branches change heavily during review. Each change cascades upward, and the top branches may never stabilize.
- Other people commit to your branches.
--force-with-leaseprotects against overwriting their pushes, but it does not make shared rewriting pleasant. Agree on who owns each branch.
Treat a stack as short-lived. A good outcome is that every branch lands within days and the stack disappears.
Quick checklist
- One idea per branch, and each branch builds and passes tests on its own.
- Three or four branches at most.
- A shared prefix and numbers in branch names.
- Fetch and restack at the start of each session.
- Verify with
merge-base --is-ancestorandrange-diffbefore pushing. - Push every rewritten branch with
--force-with-lease. - Land bottom first, retarget the next pull request, restack with
--onto, and delete the merged branch.
Exercise
You finish a restack and the status script reports STALE parser/2-tests does not contain parser/1-skeleton. The rebase itself reported no errors. Name two likely causes and one command to inspect the problem.
Show answer
Likely causes: parser/2-tests was checked out in another worktree, so --update-refs skipped it. Or --update-refs was not used (and rebase.updateRefs was not set), so only the top branch moved. Inspect with git log --oneline --decorate --graph --all or git worktree list. Fix by checking the branch out where it is allowed to move, then rebasing it onto parser/1-skeleton, for example git rebase parser/1-skeleton parser/2-tests, and then restacking parser/3-cli-wiring with git rebase --update-refs --onto parser/2-tests <old-parser-2-tip> parser/3-cli-wiring, or recover the old tips from the reflog.
Exercise
A reviewer asks for a change to commit 1a2b3c4 in parser/1-skeleton, and you are on parser/3-cli-wiring. Give the two commands that record the fix at the tip and fold it into the right commit while keeping every branch in the stack in place. Then say which branches you must push.
Show answer
git commit --fixup 1a2b3c4 followed by git rebase -i --autosquash --update-refs origin/main. The fixup moves directly after 1a2b3c4, and the update-ref lines keep each branch name on its own commit. All three branches (parser/1-skeleton, parser/2-tests, parser/3-cli-wiring) get new commit IDs, so push them all with git push --force-with-lease origin parser/1-skeleton parser/2-tests parser/3-cli-wiring.
Exercise
Why does git branch -d parser/1-skeleton refuse to delete a branch that was squash-merged into main, and what should you check before using -D?
Show answer
A squash merge creates a new commit on main with different content hashes than the branch's commits, so Git cannot see the branch tip as reachable from main and considers it unmerged. Before git branch -D, confirm on the remote (git fetch --prune origin, then look at git log origin/main) that the squash commit for that pull request is in main.