GitHub stacked pull requests

Agents finish a feature in one session. You open one pull request for that feature. That pull request contains the whole feature. Reviewers do not read all of the code. The pull request stays unmerged.

A stack splits the feature into a chain of pull requests. All branches stay in the same repository. Each pull request uses the branch below it as its base. Reviewers then see the changes for one pull request.

What a stack is

Text
   ┌── feat/frontend     → PR #3 (base: feat/api-endpoints)  ← top
  ┌── feat/api-endpoints → PR #2 (base: feat/auth-layer)
 ┌── feat/auth-layer     → PR #1 (base: main)               ← bottom
main

The figure shows one stack. The trunk is the base of the bottom pull request. In this figure, the trunk is main. Each pull request above the bottom pull request uses the branch below it as its base.

The bottom pull request contains the schema. The middle pull request contains the API. The top pull request contains the UI.

A concern is one subject of the work.

  1. If code in pull request A depends on code in pull request B, put B in the same pull request.
  2. If B is not in pull request A, put B in a lower pull request.
  3. When the concern changes, start a new branch.
  4. When the current pull request is large enough for one review, start a new branch.

Stacked pull requests are in public preview. The commands can change during public preview. All branches of a stack are in one repository. A stack does not include a fork. GitHub Desktop does not open a stack.

Create a stack

  1. If you do not have GitHub CLI 2.90.0 or a later version, install that version.
  2. If you do not have Git 2.36 or a later version, install that version.
  3. Run gh auth login.
  4. Run gh extension install github/gh-stack.
  5. Run gh stack init feat/auth-layer.
  6. Run git add ..
  7. Run git commit -m "Add auth data model".
  8. Run gh stack add feat/api-endpoints.
  9. Run git add ..
  10. Run git commit -m "Add auth API routes".
  11. Run gh stack submit.
Bash
gh auth login
gh extension install github/gh-stack
gh stack init feat/auth-layer
git add .
git commit -m "Add auth data model"
gh stack add feat/api-endpoints
git add .
git commit -m "Add auth API routes"
gh stack submit

The first pull request uses the trunk as its base. The next pull request uses branch feat/auth-layer as its base. Each pull request shows only the changes on its branch.

Merge the stack

A stack merges from the bottom pull request toward the top pull request. You cannot merge a middle pull request without the pull requests below it.

  1. Run gh stack merge.
  2. Do not run gh pr merge.
  3. Do not squash a pull request with the green "Merge" button.
Bash
gh stack merge 35 --yes --squash
gh stack merge 23 --yes --squash

gh stack merge with no number merges the current stack. A number that matches a stack merges that stack. A pull request number merges that pull request and every pull request below it. The two example commands follow this number rule.

When you merge the top pull request, the whole stack lands on the trunk. When you merge a middle pull request, that pull request lands on the trunk. Every pull request below that pull request also lands on the trunk. GitHub keeps the pull requests above that pull request. GitHub then sets the base of those pull requests to the trunk.

The gh stack merge operation is all-or-nothing. If one pull request in the group cannot merge, no pull request in the group merges.

A merge queue is available for a stack. GitHub adds the pull requests to the merge queue from the bottom toward the top. When the trunk uses a merge queue, gh stack merge does not use the --squash flag. The queue selects the merge method. The selected pull requests can land in more than one group. If one pull request leaves the merge queue, every pull request above it also leaves the queue.

After you merge with gh stack merge, GitHub rebases the next pull request onto the trunk. Your local branches can differ from the trunk after that rebase.

  1. Before you read the next pull request, run gh stack sync --prune.
Terminal

gh stack sync --prune fetches the trunk. The command skips each merged pull request. The command rebases the other branches onto the trunk with --onto. The command then pushes those branches with --force-with-lease. The --prune flag deletes the local branches for merged pull requests. If the local stack and the GitHub stack differ, gh stack sync stops before it pushes.

If you do not run this command, the next pull request shows "CONFLICTING".

The squash-replay conflict

If you squash the bottom pull request with the green "Merge" button, GitHub writes a new commit on the trunk. The original SHA of that pull request is gone. GitHub sets the base of the next pull request to the trunk. That pull request still contains the original commits. Git reports "added in both" for each file that both pull requests change. The intent of the two commits is the same.

But the SHAs differ. This message is not a conflict in the file contents.

gh stack merge drops the squashed commits from the next pull request. gh stack merge then rebases that pull request onto the trunk. The green "Merge" button only changes the base of the next pull request. The green "Merge" button does not drop the old commits.

Lock branch stops a stack merge

Keep these rules on the trunk:

  • Required reviews
  • Required status checks
  • A linear history
  • Conversation resolution
  • No force-push
  • No deletion of the default branch

These rules apply to every pull request in the stack. gh stack merge applies the rules when you run the command. gh stack merge cannot ignore the rules. Auto-merge is not available for a stack.

Lock branch is a classic protection rule. A ruleset rule with the name "Restrict updates" also stops an update to the branch ref. You can still squash one pull request onto a locked trunk. gh stack merge writes the trunk through the async stack-merge API. The command fails with "Cannot change this locked branch". A repository administrator gets the same error.

gh stack merge has no administrator override.

Result

Agents generate a large amount of code. A stack gives each concern one pull request.

  1. Merge with gh stack merge.
  2. After you merge part of the stack, run gh stack sync --prune before you open the next pull request.
  3. Do not squash a pull request with the green "Merge" button.

References

  1. GitHub - docs.github.com/en/pull-requests/get-started/about-stacked-prs
  2. GitHub - docs.github.com/en/pull-requests/get-started/stacked-prs-quickstart
  3. GitHub - docs.github.com/en/pull-requests/how-tos/merge-and-close-pull-requests/merging-stacked-pull-requests
  4. GitHub - docs.github.com/en/pull-requests/how-tos/create-pull-requests/managing-stacked-pull-requests
  5. GitHub - docs.github.com/en/pull-requests/how-tos/merge-and-close-pull-requests/troubleshooting-stacked-pull-requests
  6. GitHub - docs.github.com/en/copilot/tutorials/stack-ai-generated-code-in-pull-requests
  7. GitHub - docs.github.com/en/pull-requests/reference/stacked-prs-cli-commands

Handmade by Devvrat. All rights reserved.