Skip to content

Support gh stack checkout (no args) to auto-detect and checkout the stack for the current branch #426

Description

@skarim

Summary

Allow gh stack checkout (no arguments) to detect whether the current local branch belongs to a stack, confirm with the user, and then checkout that stack locally.

Background

When working in a fresh Codespace (or any environment) developers often git checkout their feature branch but forget to check out the corresponding stack. Running gh stack add currently emits a message like:

✗ current branch "overview-comments/new-view-controls" is not part of a stack
Checkout an existing stack using gh stack checkout or create a new stack using gh stack init

This makes me expect that running gh stack checkout (without args) might look up the stack for the current branch. It would be convenient if the command did that automatically.

User story / expected behavior

  • When the user runs gh stack checkout with no arguments, the CLI should call the list stacks API and determine whether the current local branch is part of a stack.
  • If the current branch is part of exactly one stack:
    • Show a confirmation message mentioning the stack (and branch), e.g.:
      "Found stack STACK_NAME that includes branch BRANCH_NAME. Checkout stack STACK_NAME? [Y/n]"
    • If the user confirms, proceed with checking out the stack locally (same behavior as gh stack checkout STACK_NAME).
    • If the user declines, abort without changes.
  • If the current branch is not part of any stack, preserve existing behavior (print the current helpful guidance suggesting gh stack checkout <stack> or gh stack init).
  • If the current branch appears in multiple stacks (edge case): present a numbered list of matching stacks and prompt the user to pick one (or cancel).

Acceptance criteria

  • gh stack checkout without args queries the list stacks API and inspects stack membership for the current git branch.
  • The command only proceeds automatically after explicit user confirmation.
  • If no matching stack is found, the command returns the same guidance/error message as today.
  • The user-facing messages are clear and match CLI UX conventions used in other gh commands.

Implementation notes / considerations

  • Use the same API that powers listing stacks to determine which stack(s) include the branch name.
  • Ensure the CLI handles remote vs local branch naming differences (e.g. origin/branch vs branch) consistently.
  • Consider non-interactive/CI usage: support a --yes/-y flag to auto-confirm when callers expect non-interactive behavior.
  • If there are multiple matching stacks, prefer presenting an interactive choice; in non-interactive mode, fail with a clear error and exit code.

Tag

checkout-auto-detect-stack

View original Slack conversation

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions