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

No non-interactive way to select a stack when the branch belongs to several (e.g. the trunk)

Open xn opened this issue 3 months ago • 1 comments

Summary

When the current branch belongs to more than one stack — most commonly the trunk, which is the base of every stack in the repo — gh stack view fails and offers no non-interactive way out:

$ gh stack view --short
✗ branch "main" belongs to multiple stacks; use an interactive terminal to select one

There is no --stack flag. Combined with gh stack view silently discarding positional arguments (#414), there is currently no way at all to view a specific stack non-interactively.

Why this matters

Two stacks off a shared trunk is the normal state for anyone working on more than one change at a time, not an edge case — and main is in that position permanently, so any script or agent that starts from the default branch hits this immediately.

"Use an interactive terminal" is not available in CI, in a shell script, or to an automated agent. The only workaround is to gh stack checkout <n> first purely to move onto a branch in the desired stack, which mutates the working tree just to perform a read.

Reproduction

# create two stacks that both base off main
git switch main
gh stack view --short      # -> "belongs to multiple stacks; use an interactive terminal"

Public repo with two stacks off a shared main: https://github.com/xn/gh-stack-repro

Expected

A --stack <number> flag on view (and anywhere else this ambiguity can arise), so the stack can be named explicitly without changing the checked-out branch.

Environment

  • gh 2.97.0
  • gh-stack v0.1.0
  • git 2.50.1
  • macOS 26.4.1, darwin/arm64

xn avatar Aug 07 '26 15:08 xn

Hit this today, and I think there's a contributing cause worth adding: fully-merged stacks are never cleaned up, and they cannot be removed.

In a repo where I'd only ever worked on single-branch stacks, .git/gh-stack had accumulated two entries, each holding one already-merged PR:

--- stack index 0: id=None number=None trunk=main@d85cdca1
    <branch-a>   pr=<#1>  merged=True

--- stack index 1: id=None number=None trunk=main@e13bd13b
    <branch-b>   pr=<#2>  merged=True

Both PRs merged, both local branches long deleted, and gh api repos/OWNER/REPO/stacks returned [] — nothing on GitHub at all. Purely local residue. But both list main as trunk, so:

$ gh stack view
✗ branch "main" belongs to multiple stacks; use an interactive terminal to select one

The part that makes it a trap rather than an annoyance: these stacks have no id and no number, so gh stack unstack <stack-number> has nothing to address them by. There is no gh stack list to enumerate them either. So the ambiguity can't be resolved or cleared from the CLI — interactive terminal or not. I ended up editing .git/gh-stack by hand to empty the stacks array.

So beyond the --stack <number> flag this issue asks for, two things would help:

  1. Prune stacks whose PRs are all merged. A stack with no open PRs and no remote counterpart shouldn't stay in the ambiguity set. This is what causes the count to creep up over time until the trunk becomes unusable.
  2. A way to address a numberless stack. Either always persist an id, or let unstack --local take an index / branch name so locally-only stacks can be removed without hand-editing internal state.

Worth noting the ambiguity here wasn't caused by having two stacks in flight — it was caused by two stacks that had been finished weeks apart and never swept up. Anyone using gh stack for a while on a single repo will drift into this.

Environment: gh 2.97.0, gh-stack v0.1.0, git 2.50.1, macOS 26.6 darwin/arm64.

danielbaars avatar Aug 12 '26 10:08 danielbaars