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 a branch is a movable pointer to a commit, and that you have opened pull requests from remote branches. You need Git 2.38 or newer for later chapters, but nothing here requires it.
The idea
A stack is a chain of branches where each branch starts from the tip of the one before it. The first branch starts from main. The second starts from the first. The third starts from the second.
Stacks help when one large change would make a poor pull request. You split the work into small steps, such as a parser, then the CLI wiring, then the docs. Each step gets its own branch and its own pull request. Reviewers read small diffs. You keep working on step two while step one is under review.
Building a stack
Create each branch from the one you are standing on. Commit work before you create the next branch.
git switch -c feature-1 main
# ...commit work...
git switch -c feature-2
# ...commit work...
git switch -c feature-3
# ...commit work...
git log --oneline --decorate --graph main..feature-3git switch -c feature-2 with no start point uses your current commit. That is why feature-2 begins exactly at the tip of feature-1.
The log shows only commits reachable from feature-3 and not from main. Here is example output:
* 0f1e2d3 (HEAD -> feature-3) Document CLI flag
* 9a8b7c6 (feature-2) Wire parser into CLI
* 5d6e7f8 Add parser tests
* 1a2b3c4 (feature-1) Add parser skeletonEach branch name labels one commit in a single straight line. The history is linear. feature-1 owns the first commit, feature-2 adds two more, and feature-3 adds one more.
Branches are labels on commits
The line above has only four commits, but three branch names. A branch is a ref, a name that points to one commit. Moving a branch means changing which commit its name points to. No commits are copied or edited when you do that.
This matters for stacks. The branches are not separate containers. They are labels placed along one line of history. The commits between two labels belong to the upper branch's pull request.
How each pull request is scoped
Each pull request targets the branch below it, not main:
| Branch | Pull request base | What the diff shows |
|---|---|---|
| `feature-1` | `main` | Commits after `main` up to `feature-1` |
| `feature-2` | `feature-1` | Commits after `feature-1` up to `feature-2` |
| `feature-3` | `feature-2` | Commits after `feature-2` up to `feature-3` |
Because of the bases, each reviewer sees only one step. Review order matters. feature-1 must be approved and merged first, because everything above it depends on it.
What can go wrong
The stack stays tidy only while main does not move. In practice main moves. A teammate merges a change, and now your stack sits on an old base. You want to rebase onto the new main.
If you rebase only feature-3, Git rewrites its commits. The labels feature-1 and feature-2 stay on the old commits. The stack is no longer a single line, and the lower pull requests no longer match the top. The next chapter looks at that problem in detail, and the chapter after it fixes it.
Exercise
You run git switch -c feature-2 while feature-1 is checked out and has two commits ahead of main. Where does feature-2 point, and what does git log main..feature-2 show?
Show answer
feature-2 points to the same commit as feature-1, because a new branch with no start point begins at the current commit. git log main..feature-2 shows the same two commits that feature-1 has. Any commits you add afterwards appear only on feature-2.
Exercise
In a stack of feature-1, feature-2, and feature-3, which base branch should the pull request for feature-3 target, and why?
Show answer
It should target feature-2. The diff then shows only the commits that feature-3 adds on top of feature-2, so the reviewer sees one step instead of the whole stack.