gh-stack icon indicating copy to clipboard operation
gh-stack copied to clipboard

[Bug]: Stack merge via merge-async lands the trunk merge commit, then fails with "Could not create merge commit" and leaves upper PRs open

Open tomassvatek opened this issue 3 weeks ago • 2 comments

Describe the bug

Merging a native stack through the asynchronous merge API landed the code on the trunk, then failed partway through. GitHub created one merge commit on the trunk that merges the top PR's head, so every commit in the stack reached the trunk. Then it marked only the two lowest PRs as merged and finished with failed: Could not create merge commit. The other six PRs stayed open even though all their commits were already on the trunk, and there was no supported way to mark them merged afterwards.

Affected command

None. The merge was a direct REST call: PUT /repos/{owner}/{repo}/pulls/{top}/merge-async with merge_method: merge and merge_action: direct_merge. Before that, gh pr merge on the bottom PR was refused with "This pull request is part of a stack and must be merged using the asynchronous merge REST API".

gh-stack version

v0.1.0 installed, but it was not used for the merge.

GitHub CLI version

gh version 2.97.0 (2026-07-31)

Operating system

macOS 26

Shell

zsh

Steps to reproduce

Private organization repository, no merge queue on the trunk, no required reviews, merge commits allowed, "Automatically delete head branches" on.

  1. Have an 8-PR native stack, trunk <- #1 <- #2 <- ... <- #8. Each PR was clean and mergeable, its required checks passed on its current head, and every branch contained the branch below it.
    • Before merging, each branch had been rebased and force-pushed several times, including one rebase of the whole stack onto the latest trunk. Each push used --force-with-lease.
  2. Call PUT /pulls/{#8}/merge-async with sha set to #8's head, merge_method: merge and merge_action: direct_merge. Response: 202, status: pending.
  3. Poll GET /pulls/{#8}/merge-async/{uuid}.

Expected behavior

Either every PR in the stack is merged and marked merged, or nothing changes, since stack merges are described as atomic.

Actual behavior

  • The trunk got one merge commit, "Merge pull request #8 from …". Its parents are the previous trunk head and #8's head, so all eight PRs' commits are on the trunk, and the trunk's tree is identical to #8's head.
  • #1 and #2 were marked merged, with that same merge commit.
  • The async request then ended as failed: Could not create merge commit. #3–#8 stayed open, still based on #2–#7's branches. GitHub kept test merges (refs/pull/N/merge) for each of them and reported them MERGEABLE/clean.
  • Retrying merge-async on #8, and then on #3 alone, with the matching sha each time: 202, then the same failed: Could not create merge commit. Nothing changed.
  • gh pr edit {#3} --base trunk is refused: "Cannot change the base branch because the pull request is part of a stack."
  • Deleting the head branches of the merged #1 and #2 did not retarget #3 to the trunk. GitHub closed #3 without merging it. Restoring the branch allowed #3 to be reopened.
  • The only way left was to close #3–#8 by hand, so they show "Closed" instead of "Merged" and integrations that react to "merged" (for example, issue trackers that close on merge) don't fire.

Possibly related: #457 (open stack PRs when the trunk already has the merge commit), #485 (stale stack base after a partial merge) and #581 (dependent PR left on a merged branch and not retargeted). Here GitHub's own stack merge created the trunk commit and then failed to finish.

Terminal output

$ gh pr merge {#1} --merge --match-head-commit <sha>
GraphQL: This pull request is part of a stack and must be merged using the asynchronous merge REST API. (mergePullRequest)

$ gh api -X PUT repos/{owner}/{repo}/pulls/{#8}/merge-async -f sha=<head sha> -f merge_method=merge -f merge_action=direct_merge
HTTP/2.0 202 Accepted
{"status":"pending","details":{"message":"Merge request enqueued.","merge_method":"merge","merge_action":"direct_merge", ...}}

$ gh api repos/{owner}/{repo}/pulls/{#8}/merge-async/<uuid>
{"status":"failed","details":{"message":"Could not create merge commit", ...}}

$ gh pr edit {#3} --base <trunk>
GraphQL: Cannot change the base branch because the pull request is part of a stack. (updatePullRequest)

The merge request UUIDs and the repository are available to GitHub staff privately on request.

Preflight checks

  • [x] I searched the existing issues for duplicates.
  • [ ] I ran gh extension upgrade stack and reproduced this issue on the latest stable release. (Not applicable: the merge ran server-side through the REST API, not through the extension.)

tomassvatek avatar Nov 10 '26 17:11 tomassvatek

We hit the same bug on a public repository, with a 2-PR native stack. In our case neither PR was marked merged.

  • Repo and stack: aleo-labs-protocol/snarkVM, stack #3880: aleo-labs-protocol/snarkVM#3866 (position 1, base staging) and aleo-labs-protocol/snarkVM#3879 (position 2). Both were approved, MERGEABLE, and had green checks. Just before merging, each branch got a plain merge of the trunk (a fast-forward push, no force-push).
  • Web UI: the merge button repeatedly showed "Could not create merge commit".
  • API: PUT /repos/aleo-labs-protocol/snarkVM/pulls/3879/merge-async with merge_method=merge, merge_action=direct_merge and sha set to #3879's head dd255664e returned pending (uuid f4b539e7-3a88-4b06-9988-399bf3f02a9e). It then ended as failed: Could not create merge commit.
  • The trunk moved anyway: staging advanced to 8691ef268, "Merge pull request #3879 from …", at 2026-11-23T20:41:09Z. Its parents are the previous trunk head 1aea93d83 and #3879's head dd255664e, so the whole stack is on the trunk.
  • The PRs stayed open: unlike the report above, neither #3866 nor #3879 was marked merged. Both stayed OPEN with mergeable_state: dirty for several minutes, so we closed them by hand.

The same merge-async call worked on earlier stacks in this repository, for example on 2026-11-11 and 2026-11-20.

cbeck88 avatar Nov 24 '26 00:11 cbeck88

We hit this too, on a public repository, merging only the bottom PR of a 5-PR native stack. GitHub marked no PR as merged. Afterwards, the stack's PRs stopped getting CI runs and their mergeability stayed UNKNOWN. Rebase stack in the web UI reported success but changed nothing.

Repo and stack. mysticcoders/reviewbot, stack #15: mysticcoders/reviewbot#12 (bottom, base main) <- #11 <- #13 <- #14 <- #10. All five were approved, MERGEABLE/CLEAN, and had green required checks. Each branch contained the one below it. main had no branch protection and no merge queue, and merge, squash and rebase were all allowed.

What we did, and what happened (times UTC, 2026-11-30):

  1. gh pr merge 12 --merge was refused: "This pull request is part of a stack and must be merged using the asynchronous merge REST API."
  2. ~19:55: PUT /repos/mysticcoders/reviewbot/pulls/12/merge-async with merge_method: merge returned pending (uuid 6037753d-11ae-4eb0-bba1-12fe63bf3b15, merge_action: default, expected_head_sha 1b88cdf). Polling returned failed within ~10 s, with "This merge could not be completed. Please try again."
    • But main did move: GitHub created merge commit ae8cb78 ("Merge pull request #12 from mysticcoders/ci/8/checks", committed 19:55:57Z), whose second parent is #12's head 1b88cdf.
  3. Retrying with merge_method: merge failed with "Could not create merge commit". So did merge_method: rebase (uuid ea7a5e5b-c33c-4cf6-ac13-80e3dff8acbb). By then mergeable_state was dirty and rebaseable false.
  4. #12 stayed open. GET /pulls/12/merge returned 404, even though main's tree was identical to #12's head tree. #11 stayed based on ci/8/checks; there was no auto-rebase or retarget.
  5. Rebase stack in the web UI appeared to succeed, but no branch moved and no push happened.
  6. No CI ran anymore for anything in the stack. The push workflow on main never ran for ae8cb78, and later force-pushes to the stack branches (from gh stack rebase + gh stack push, v0.3.0) triggered no pull_request runs. mergeStateStatus stayed UNKNOWN on all four open PRs. Actions was enabled, the workflow was active, and githubstatus.com was all green. A normal, unstacked PR opened afterwards in the same repo got CI and merged without trouble.
  7. gh pr edit 11 --base main was refused: "Cannot change the base branch because the pull request is part of a stack."

How we got out. We closed #12 by hand. We rebased the remaining four branches onto main with gh stack rebase and pushed them with gh stack push; each new head's tree was identical to the CI-tested head before the rebase. Then we fast-forwarded main to the top of the stack with a plain git push, closed #11, #13, #14 and #10 by hand, and ran gh stack unstack 15.

Expected. Either the merge fails before writing to the trunk, or, once the trunk merge commit exists, GitHub records #12 as merged and continues the stack: it retargets #11 to main, rebases the rest, and keeps triggering workflows. Also, when the trunk already contains a PR's head (as in #457), GitHub could recognize the PR as merged.

Versions. gh 2.102.1, gh-stack v0.3.0 (installed after the merge attempt, used only for rebase, push, view and unstack), macOS 27.0.1, zsh. The merge itself was a direct REST call through gh api.

kinabalu avatar Dec 01 '26 03:12 kinabalu