Skip to content

update add cmd to adopt existing branches#101

Draft
skarim wants to merge 1 commit into
skarim/prompter-improvementsfrom
skarim/add-existing-branch
Draft

update add cmd to adopt existing branches#101
skarim wants to merge 1 commit into
skarim/prompter-improvementsfrom
skarim/add-existing-branch

Conversation

@skarim
Copy link
Copy Markdown
Collaborator

@skarim skarim commented May 19, 2026

Allow gh stack add to adopt existing branches

Previously, gh stack add rejected any branch name that already existed
in git with a blanket "branch already exists" error. This was overly
restrictive — users who create branches ahead of time (e.g. from the
GitHub UI or via git branch) had no way to incorporate them into a
stack without deleting and recreating them.

Now, if the specified branch exists in git but is not part of any
existing stack, add adopts it: it skips branch creation, checks out
the existing branch, and appends it to the stack metadata. This mirrors
the adopt-or-create pattern already used by gh stack init.

Branches that belong to another stack are still rejected by the existing
ValidateNoDuplicateBranch guard, so there is no risk of cross-stack
conflicts.

Behavioral summary:

  • Existing branch, not in any stack → adopted (checkout only, no create)
  • Existing branch, already in a stack → error (unchanged)
  • Non-existent branch → created (unchanged)
  • Staging/commit flags (-A, -u, -m) work with adopted branches

Tests added:

  • TestAdd_AdoptsExistingBranch
  • TestAdd_RejectsExistingBranchInStack
  • TestAdd_AdoptsExistingBranchWithCommit

Stack created with GitHub Stacks CLIGive Feedback 💬

Previously, `gh stack add` rejected any branch name that already existed
in git with a blanket "branch already exists" error. This was overly
restrictive — users who create branches ahead of time (e.g. from the
GitHub UI or via `git branch`) had no way to incorporate them into a
stack without deleting and recreating them.

Now, if the specified branch exists in git but is not part of any
existing stack, `add` adopts it: it skips branch creation, checks out
the existing branch, and appends it to the stack metadata. This mirrors
the adopt-or-create pattern already used by `gh stack init`.

Branches that belong to another stack are still rejected by the existing
`ValidateNoDuplicateBranch` guard, so there is no risk of cross-stack
conflicts.

Behavioral summary:
- Existing branch, not in any stack → adopted (checkout only, no create)
- Existing branch, already in a stack → error (unchanged)
- Non-existent branch → created (unchanged)
- Staging/commit flags (-A, -u, -m) work with adopted branches

Tests added:
- TestAdd_AdoptsExistingBranch
- TestAdd_RejectsExistingBranchInStack
- TestAdd_AdoptsExistingBranchWithCommit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant