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
┌── 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
mainThe 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.
- If code in pull request A depends on code in pull request B, put B in the same pull request.
- If B is not in pull request A, put B in a lower pull request.
- When the concern changes, start a new branch.
- 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
- If you do not have GitHub CLI 2.90.0 or a later version, install that version.
- If you do not have Git 2.36 or a later version, install that version.
- Run
gh auth login. - Run
gh extension install github/gh-stack. - Run
gh stack init feat/auth-layer. - Run
git add .. - Run
git commit -m "Add auth data model". - Run
gh stack add feat/api-endpoints. - Run
git add .. - Run
git commit -m "Add auth API routes". - Run
gh stack submit.
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 submitThe 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.
CAUTION
Do not run git rebase on a stack branch. This command can break the stack. Do not run git push --force on a stack branch. This command can break the stack. Run gh stack rebase. Run gh stack push.
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.
- Run
gh stack merge. - Do not run
gh pr merge. - Do not squash a pull request with the green "Merge" button.
gh stack merge 35 --yes --squash
gh stack merge 23 --yes --squashgh 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.
CAUTION
If gh stack merge fails, do a check of the trunk before you run the command again. Pull requests below the failure can already be on the trunk.
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.
- Before you read the next pull request, run
gh stack sync --prune.
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.
CAUTION
Do not resolve this conflict in the GitHub UI. Run gh stack sync --prune. This command rebases the next pull request onto the trunk.
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.
CAUTION
Use Lock branch for a release freeze. Use Lock branch for an incident. Remove the lock before you merge. If you cannot remove the lock, wait. Do not squash a pull request with the green "Merge" button while the branch is locked. This action causes the squash-replay conflict.
Result
Agents generate a large amount of code. A stack gives each concern one pull request.
- Merge with
gh stack merge. - After you merge part of the stack, run
gh stack sync --prunebefore you open the next pull request. - Do not squash a pull request with the green "Merge" button.
References
- GitHub - docs.github.com/en/pull-requests/get-started/about-stacked-prs
- GitHub - docs.github.com/en/pull-requests/get-started/stacked-prs-quickstart
- GitHub - docs.github.com/en/pull-requests/how-tos/merge-and-close-pull-requests/merging-stacked-pull-requests
- GitHub - docs.github.com/en/pull-requests/how-tos/create-pull-requests/managing-stacked-pull-requests
- GitHub - docs.github.com/en/pull-requests/how-tos/merge-and-close-pull-requests/troubleshooting-stacked-pull-requests
- GitHub - docs.github.com/en/copilot/tutorials/stack-ai-generated-code-in-pull-requests
- GitHub - docs.github.com/en/pull-requests/reference/stacked-prs-cli-commands