Sub topic 2
What a stack of branches looks like
This chapter assumes you can add, commit, switch, and read git log. It also assumes you know that a branch is a movable pointer to a commit, and that you understand pull requests and remote branches. Commands use git switch, and the later chapters need Git 2.38 or newer, so check git --version now.
The idea in one paragraph
A stack of branches is a chain of branches where each one is created from the tip of the previous one. The first branch starts from main. The second starts from the first. The third starts from the second. Each branch holds one small, self-contained step of a larger change. You open a pull request for each step, so a reviewer sees a few dozen lines at a time and not one 2,000-line diff.
Why people stack branches
Suppose you are adding a parser to a command-line tool. The work has three natural pieces: the parser itself, the wiring into the CLI, and the documentation. You could put everything on one branch and open one large pull request. Reviewers tend to skim large diffs, and one objection to any part blocks all of it.
You could also finish the parser, open a pull request, and wait for approval before starting the CLI work. That keeps pull requests small but leaves you idle.
Stacking avoids both problems. You start the next piece on top of the previous one while the earlier pull request is still in review. Stacking helps when:
- The change is large but splits into steps that each make sense alone.
- Review turnaround is slow and you don't want to wait on it.
- Later steps depend on code from earlier steps, so they can't start from
main. - You want reviewers to read refactoring, new behavior, and docs separately.
Stacking is not free. Every branch below the top is a place where a change can ripple upward, and the rest of this tutorial deals with that cost. If your pieces don't depend on each other, use independent branches from main and skip the stack.
Building a stack
This script creates a small repository with a three-branch stack. You can run it as shown. It assumes your Git user.name and user.email are already configured.
git init -b main stack-demo
cd stack-demo
git commit --allow-empty -m "Initial commit"
git switch -c feature-1 main
echo "parser skeleton" > parser.txt
git add parser.txt
git commit -m "Add parser skeleton"
git switch -c feature-2
echo "cli flag" > cli.txt
git add cli.txt
git commit -m "Add CLI flag"
git switch -c feature-3
echo "docs" > docs.txt
git add docs.txt
git commit -m "Document CLI flag"
git log --oneline --decorate --graph main..feature-3Look at how each git switch -c is called. The first names main as the starting point. The other two name nothing, so Git starts the new branch at the current HEAD. That current HEAD is the tip of the branch you were just on, and that is what makes the chain.
The log output looks like this. Your commit IDs will differ.
* 7c1d9e2 (HEAD -> feature-3) Document CLI flag
* 4b8a0f3 (feature-2) Add CLI flag
* e91c5a7 (feature-1) Add parser skeletonAll three branch names appear on one straight line of history. The branches differ only in how far along that line they point.
The shape of the stack
A diagram makes the structure clearer than the log does.
Two points follow from this picture.
First, feature-3 contains everything. Its history includes the commits from feature-1 and feature-2. The branch name feature-3 points at the last commit, but git log feature-3 shows the whole chain back to main.
Second, each branch owns only the commits above the branch below it. When you talk about what feature-2 "contains" as a unit of review, you mean the commits between feature-1 and feature-2, not its full history.
Branches are labels on a line
This is the point from the first chapter of this tutorial, applied here. A branch is a ref, a name that holds one commit ID. In a stack, the refs happen to sit at different heights of the same line of commits.
Git does not record that feature-2 is "based on" feature-1. Nothing in the repository says so. The relationship is implied by the commit graph: feature-1's tip is an ancestor of feature-2's tip. You can ask Git about it directly.
git merge-base --is-ancestor feature-1 feature-2 && echo "feature-1 is below feature-2"The command prints nothing on failure and sets a non-zero exit status. This matters later. When commits get rewritten, the ancestor relationship can silently break, and Git will not warn you that your stack has come apart.
Reading one layer at a time
The two-dot range A..B means "commits reachable from B but not from A". Use it to see exactly what each branch adds.
git log --oneline main..feature-1
git log --oneline feature-1..feature-2
git log --oneline feature-2..feature-3Each command prints the commits for one layer. You can count them too:
git rev-list --count feature-1..feature-2These ranges are the unit of work in a stack. A pull request for feature-2 should show the output of feature-1..feature-2, and nothing else. The same range syntax will return when we move commits with --onto.
| Branch | Built on | Commits it adds | Pull request base |
|---|---|---|---|
| `feature-1` | `main` | `main..feature-1` | `main` |
| `feature-2` | `feature-1` | `feature-1..feature-2` | `feature-1` |
| `feature-3` | `feature-2` | `feature-2..feature-3` | `feature-2` |
Pull requests in a stack
The last column of the table is the part that makes a stack reviewable. If you open a pull request from feature-2 with main as its base, the diff includes the commits from feature-1 as well, and the reviewer sees the same code twice. Set each pull request's base to the branch below it.
git push -u origin feature-1 feature-2 feature-3After pushing, create the pull requests in your hosting service. Target main for the first, feature-1 for the second, and feature-2 for the third. Most hosts then show only the layer's own changes.
Merge order is bottom to top. feature-1 goes into main first. After that, the second pull request needs a new base, either main or whatever its host retargets it to. Many hosts retarget automatically when the base branch is merged and deleted. How the first branch gets merged (merge commit, rebase, or squash) decides how much work the rest of the stack needs. Squash merges are the hard case, and a later chapter covers them.
A stack is a line, not a tree
A stack has exactly one branch at each level. If you create two branches from feature-1, you have a tree, not a stack.
git switch feature-1
git switch -c feature-1-altNow feature-2 and feature-1-alt both sit directly on feature-1. Nothing is wrong with that, and it is often useful for trying an alternative. But the tools in this tutorial are designed for a line. Rebasing the top branch of one fork leaves the other fork behind, which is the same stale-branch problem the next chapter describes. If you need a tree, expect to handle each limb by hand.
What goes wrong when a lower branch changes
The stack is stable only while the lower branches stay put. Three events change them:
- A reviewer asks for a change in
feature-1. You commit a fix onfeature-1, and nowfeature-2andfeature-3no longer sit on top of its tip. mainmoves forward because other people merged work. Your stack still sits on the oldmain.- You amend or reorder a commit in a lower branch, which gives that commit a new ID and leaves the upper branches built on the old one.
In each case the upper branches still point at commits that descend from an older version of the lower branch. You can see this after adding a fix to feature-1:
git switch feature-1
echo "fix" >> parser.txt
git commit -am "Fix parser edge case"
git log --oneline --graph --decorate --allThe graph now forks. feature-1 has moved one commit ahead, while feature-2 and feature-3 still hang from the old feature-1 commit. The stack has become a tree by accident, and the "feature-2 is built on feature-1" statement is no longer true. Restoring the line is the whole job of the rest of this tutorial.
Habits that make stacks easier
You can keep a stack manageable from the start with a few conventions.
- Name branches so their order is visible. Names like
parser-1-skeleton,parser-2-cli,parser-3-docssort correctly ingit branchoutput and make the order obvious to reviewers. - Keep layers small. A layer that needs more than a few commits or a long review is a sign it should split further.
- Make each layer pass tests on its own. A reviewer, or a bisect later, may check out any single layer.
- Keep stacks short. Three to five branches is common. Each extra layer multiplies the places where a change below forces work above.
- Fix problems in the layer where they belong. Don't patch a parser bug in
feature-3because you happen to be there. A fix on the wrong branch confuses the review of both. - Check the shape before and after any history change. The
git log --oneline --decorate --graphcommand from earlier works as a quick sanity check.
Exercises
Exercise
In a stack of main, feature-1, feature-2, and feature-3, which command shows only the commits that belong to the feature-2 pull request, and what should that pull request use as its base branch?
Show answer
git log --oneline feature-1..feature-2 shows only those commits. The pull request base should be feature-1, so the diff excludes the commits from the layer below.
Exercise
You are on feature-2 and run git switch -c feature-3. Where does feature-3 start, and why does that matter for a stack?
Show answer
With no starting point given, Git creates the branch at the current HEAD, which is the tip of feature-2. That is what makes feature-3 sit on top of feature-2 and form the chain. If you had named main as the starting point, you would have created an independent branch instead of a stack layer.
Exercise
After you add a new commit to feature-1, feature-2 and feature-3 are unchanged. Why is the stack no longer a straight line, and what does the log graph show?
Show answer
feature-2 still descends from the old tip of feature-1, so the new feature-1 commit is not an ancestor of it. The graph forks at that old commit: feature-1 continues on one side, and feature-2 and feature-3 continue on the other.