fix: resolve ambiguous remote branch right after base branch selection - #325
Merged
Conversation
Two problems with the ambiguous-remote-branch confirmation: - It appeared even when the user had explicitly selected a base branch: createWorktreeEffect resolved the NEW branch name against remotes, so a name existing on multiple remotes failed with AmbiguousBranchError even though the base branch (e.g. a local branch) was already chosen. - It appeared only at the very end of the wizard (after creation mode, branch name, and copy settings), because ambiguity was detected when the worktree was actually created. Changes: - Add WorktreeService.resolveBaseBranch() which classifies a selected base branch (local / single remote / ambiguous / none) without throwing. - NewWorktree resolves the base branch immediately after selection and, when ambiguous, shows RemoteBranchSelector right away as a wizard step; local branches are confirmed instantly and never hit the confirmation. - createWorktreeEffect no longer fails when the new branch name exists on multiple remotes: the user already picked the base branch, so the new branch is created from it instead. - 'Use existing base branch' now creates the local branch under its short name when the base resolves to a remote ref (previously a branch literally named "origin/..." could be created). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
kbwo
marked this pull request as ready for review
August 19, 2026 13:30
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The "⚠️ Ambiguous Branch Reference" confirmation (
RemoteBranchSelector, shown when a branch name exists on multiple remotes) has two problems in the new-worktree wizard:AmbiguousBranchErrorand shows the confirmation — even though the base branch was already chosen.git worktree addwas about to run, i.e. after the user had already gone through creation mode, branch name, and copy settings. If the selected base branch is ambiguous, the user should be asked right after selecting it.Investigation
Facts
Reproduced on a scratch repo with two remotes (
origin,upstream) that both havefeature/xandfeature/x2, no local refs for them, and a localmain:createWorktreeEffect('../wtA', 'feature/x2', 'main')failed withAmbiguous branch 'feature/x2' found in multiple remotes: origin/feature/x2, upstream/feature/x2. Please specify which remote to use.even though the base branchmainis local. The throw comes from resolving the new branch name (resolveBranchReferenceEffect(branch)) increateWorktreeEffect, not from the base branch.AmbiguousBranchErrorhandler aftercreateWorktreeEffectfailed — which is why it always showed up at the end of the wizard rather than at branch selection.getAllBranchesEffect()merges remote branches with theorigin/prefix stripped, so a remote-only branch is indistinguishable from a local one in the list; selecting such an entry also ran into the late confirmation.Findings
The fix moves disambiguation to selection time and stops treating the new branch name's ambiguity as an error:
WorktreeService.resolveBaseBranch()classifies a selected base branch (local/ singleremote/ambiguous/none) without throwing.NewWorktreecalls it right after the base-branch selection: a local branch is confirmed instantly (never routed to the confirmation), and an ambiguous one showsRemoteBranchSelectorimmediately as a wizard step.createWorktreeEffectno longer fails when the new branch name exists on multiple remotes — the user already picked the base branch, so the new branch is created from it. The single-remote checkout-by-name behavior (typingfeature/xchecks outorigin/feature/x) is unchanged.origin/...could be created.AmbiguousBranchErrorhandling is kept as a fallback for callers that pass an unresolved base branch.Verification
Prerequisites — build this branch and create a scratch repo whose branch
feature/xexists on two remotes but not locally:Expected output of the final
git branch -a: onlymainas a local branch;feature/xandfeature/x2appear only underremotes/origin/andremotes/upstream/.Steps:
bun run lint,bun run typecheck, andbun run testall pass (1823 tests, including new unit tests forresolveBaseBranchand the creation-time fallback).createWorktreeEffect(path, 'feature/x2', 'main')succeeds andgit worktree listshows the new worktree on branchfeature/x2created frommain's commit (before this change it failed withAmbiguousBranchError)./tmp/ccm-ambiguous/work, runnode <ccmanager-checkout>/dist/cli.js, choose "New Worktree", enter a path, and selectfeature/xin "Select base branch". Expected: the "origin/feature/xandupstream/feature/xappears immediately after this selection, before the "How do you want to create the new worktree?" step. Selectingorigin/feature/xcontinues the wizard showingBase branch: origin/feature/x.mainas base branch, choose "Choose the branch name yourself" → "Create new branch from base branch", and enterfeature/x2as the new branch name. Expected: the ambiguous confirmation never appears, and after completing the wizardgit worktree listinworkshows a worktree on branchfeature/x2whose commit equalsgit rev-parse main.🤖 Generated with Claude Code