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

`gh stack sync` can't be run anywhere in the repository

Open jamietanna opened this issue 3 months ago • 1 comments

If you're on a repository that uses Stacked PRs, but you're not on a branch that's currently stacked, you see:

% gh stack sync
✗ current branch "fix/number" is not part of a stack
Checkout an existing stack using `gh stack checkout` or create a new stack using `gh stack init`

This means that if you want to leverage .git/gh-stack as the internal state for what branches are available, you need to now go onto a stacked branch to sync.

There should be a way to sync without necessarily being on a stacked branch (for instance, if you want to make sure that your local branch is cleaned up after it's been merged)

jamietanna avatar Aug 26 '26 14:08 jamietanna

The root cause is that gh stack sync begins with loadStack(cfg, ""), which resolves a stack only if the current branch belongs to one. When the current branch is not part of any stack, there is no way to indicate which stack to sync.

A possible fix is to let gh stack sync accept an optional target, similar to gh stack merge and gh stack unstack:

gh stack sync            # current behavior: sync the active stack
gh stack sync 7          # sync stack #7
gh stack sync 42         # sync the stack containing PR #42
gh stack sync feature-a  # sync the stack containing branch feature-a

If no target is provided and the current branch is not in a stack, an interactive terminal could list the locally tracked stacks and prompt for one.

A --all flag could also be added later to sync every tracked stack at once, but starting with an explicit target keeps the change small and safe.

This preserves the current stack-scoped default behavior while allowing sync to run from any branch, which addresses the cleanup-after-merge use case described in the issue. It also works in non-interactive environments such as CI.

Happy to work on a patch if this direction sounds good to the maintainers.

sdOps avatar Aug 27 '26 00:08 sdOps