Skip to content
Source
Sign inSign up

Split into a stack

Issues and pull requests / Stacks and private work

Split a change into a stack

Your change is too big for one review, so you want small pull requests that each reviewer reads in a few minutes. When you fix the first one, you do not want to rebase and force-push each branch above it.

7 min read

Open each pull request on the branch of the one before it. The forge links them into a stack, and merges each push up the stack for you.

The steps use src, and a coding agent runs the same commands. To review and merge a stack on the web, see Review and merge a stack of pull requests.

1. Open the bottom pull request

Make a branch for the first part of the change, and commit it. Then push it and open its pull request:

git switch -c parser
git push -u origin HEAD
src pr create --fill

This pull request goes into main, as usual.

2. Open each next pull request on the one before it

Make the next branch from the one before it, and commit the next part. Then open its pull request with --base:

git switch -c use-parser
git push -u origin HEAD
src pr create --fill --base parser
  • A pull request is in a stack when its base branch is the head branch of another open pull request.
  • Both head branches must be in this repository. A branch of a fork never makes a stack.
  • Do this again for each part. Each pull request uses the branch of the one below it as its base.

3. Read the stack

src pr stack view
Stack of 2 pull requests in you/app, bottom first:
  #1 Add the parser [parser -> main]
* #2 Use the parser [use-parser -> parser]
  • * marks the pull request of your branch. Give a number to read another one: src pr stack view 1.
  • --json members,below gives the stack as JSON, for a script or an agent.
  • A pull request in no stack prints is not in a stack.

When the stack forks

Two pull requests can sit on the same one. Then the stack forks, and src pr stack view shows one path up. The member where it forks names the others:

Stack of 2 pull requests in you/app, bottom first:
  #1 Add the parser [parser -> main] (stack forks: also under #3)
* #2 Use the parser [use-parser -> parser]
The stack forks. To see another path, run: src pr stack view 3

To make one path again, move a pull request with --base. See Move a pull request in a stack.

4. Push a fix to any pull request

A reviewer asks for a change in #1. Fix it on its branch, then push:

git switch parser
git commit -am "Handle empty input"
git push

The forge merges the new head of #1 into #2, then #2 into the next one, to the top of the stack:

  • Each step is a merge commit by the bot, with a message such as Merge #1 into #2 (stack sync).
  • No branch is rebased, and nothing is force-pushed.
  • CI runs on each head that moves.
  • The steps go through a queue, so they take a moment.

Before you work on an upper branch again, get the merge commits:

git switch use-parser
git pull

5. Fix a sync conflict

When your fix conflicts with a branch above it, the sync stops at that pull request. src pr stack view marks it:

* #2 Use the parser [use-parser -> parser] (stack needs update: conflict with #1)
  1. Switch to the branch of #2, and get its latest commits: git switch use-parser, then git pull.
  2. Merge the branch below it: git merge origin/parser.
  3. Fix the conflicts, commit, then push.

Your push makes the forge sync #2 again, and the pull requests above it. When the sync works, the mark goes away.

Tip

If the factory step Resolve merge conflicts is on, it gets a run on #2 and merges the branch below for you. Its agent, Keep green, is off by default.

6. Get the stack merged

A person with write access merges the stack. One merge of the top pull request lands each pull request below it first, from the bottom. See Merge the stack.

An agent never merges into the default branch.

Done

Your change is a stack of small pull requests. Each reviewer reads one part, in order. A fix in any pull request reaches the ones above it as merge commits, so git pull keeps working and nobody force-pushes.

Later: move a pull request in a stack

You want a new part between #1 and #2. Open its pull request on the branch of #1, then give #2 the new branch as its base:

git switch parser
git switch -c parser-tests
git push -u origin HEAD
src pr create --fill --base parser
src pr edit 2 --base parser-tests

The stack is now #1, then the new pull request, then #2. Before the edit it forked at #1.

  • When the new base is the head branch of an open pull request, the forge links #2 on it. It merges that head into #2 at once, as a stack sync.
  • --base main takes a pull request out of its stack. Nothing is merged into it.
  • The pull request keeps its number, its reviews and its comments. Its page shows "changed the base branch from parser to parser-tests".
  • It leaves the merge queue, and merge when ready turns off, because both were for the old base.
  • The author of the pull request can do this. So can people with write access. A pull request that is on GitHub changes its base on GitHub.

Important

The base must be a branch of this repository. The forge refuses these branches as the base:

  • The head branch of the pull request itself.
  • A branch that another open pull request from the same head goes into.
  • A branch above it in its stack.

Later: close a part and delete its branch

src pr close 2 --delete-branch
  • It closes #2, then deletes its branch: the local one, and the one on the forge. When your current branch is that branch, it switches to the default branch first.
  • Deleting the branch needs write access.
  • The forge keeps the branch while an open pull request has it as its base or its head. Move that pull request first, with src pr edit <number> --base <branch>, or close it.
  • With --repo, only the branch on the forge is deleted. A branch of a fork is never deleted.
  • For a pull request that sync brought from GitHub, it deletes the GitHub head branch on the forge. It does not delete the branch on GitHub. For a pull request from a fork on GitHub, the forge refuses.
src pr stack unlink 2
src pr stack link 2
src pr stack break 2
  • unlink stops the sync from the pull request below #2. No branch moves, and the base stays. #2 cannot merge until the pull request below it lands.
  • link turns that sync on again, and merges the head of the pull request below into #2 at once.
  • break unlinks every link of the stack of #2. Each pull request keeps its base.
  • The author of the pull request can do these. So can people with write access.

Later: REST and MCP

An agent or a script does the same work over REST or MCP, with the same rules. The REST paths are under /api/v1/repos/{owner}/{repo}.

You want toRESTMCP tool
Read the stack of a pull requestGET /pulls/{n}/stackget_pull_stack
Stop the sync from the pull request belowPOST /pulls/{n}/stack/unlinkunlink_pull_stack
Turn the sync on againPOST /pulls/{n}/stack/linklink_pull_stack
Unlink every link of the stackPOST /pulls/{n}/stack/breakbreak_pull_stack
Change the base branchPATCH /pulls/{n} with {"base": "parser-tests"}update_issue with base
Close and delete the head branchPATCH /pulls/{n} with {"state": "closed", "deleteBranch": true}update_issue with state and deleteBranch
Merge a pull request and the ones below itPUT /pulls/{n}/mergemerge_pull_request
  • The stack is members, bottom first, and below. A pull request in no stack has no members. below is the pull request that the bottom one sits on through an unlinked link, or null.
  • Each member has number, title, headRef, baseRef, draft, linkedBelow, conflict and forks.
  • conflict is the number of the lower pull request that did not merge in, or null.
  • forks lists the other linked pull requests on this member, where the stack forks. It leaves out the ones that you cannot read.
  • The merge answer has landed: each pull request that landed, bottom first.