Repository navigation
feat(worktree): reuse an existing branch when creating a worktree (#5181) - #5781
Conversation
) Adds an explicit "Reuse this branch" checkbox to the new-worktree composer. When an existing local branch is selected in the smart name field, the checkbox (default on) checks that branch out instead of creating a new branch from it — and the choice now survives renaming the worktree folder. Previously, reusing an existing branch only worked implicitly: it required keeping the auto-filled worktree name equal to the branch, and editing the name silently reverted to "create a new branch". There was no discoverable, durable control — the gap the issue reports. The fix is renderer-only: picking an existing local branch already sets baseBranch === branchNameOverride, so the existing backend reuse path (`git worktree add <path> <branch>`, preserveBranchOnDelete) checks the branch out correctly across local/SSH/runtime. The checkbox pins that override via the existing branchNameOverridePreservesNameEdits flag so the worktree folder can be named independently while the branch is reused; unchecking creates a fresh branch from the selected ref as base. - New pure helper resolveComposerBranchReuse (local-vs-remote + default). - Checkbox hidden for remote-only refs and non-branch sources. - Tests: pure helper, card render/toggle, and a backend integration test proving a renamed folder still reuses the exact branch.
|
Ready to review this PR? Stage has broken it down into 5 individual chapters for you: Chapters generated by Stage for commit dbba976 on Jun 19, 2026 9:42am UTC. |
Verification evidence1. Real git: reusing a diverged branch into an independently‑named folderThe core capability — This is exactly what the composer produces when "Reuse this branch" is checked: 2. Automated tests (project vitest config)Covering:
3. No regressions4. Static checks
|
📝 WalkthroughWalkthroughThis PR introduces a "Reuse this branch" checkbox in the workspace composer UI. Three new shared functions in 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 50337285-f5ec-49f9-9348-88f16c46b84d
📒 Files selected for processing (12)
src/main/ipc/worktrees.test.tssrc/renderer/src/components/NewWorkspaceComposerCard.test.tsxsrc/renderer/src/components/NewWorkspaceComposerCard.tsxsrc/renderer/src/hooks/composer-branch-selection.test.tssrc/renderer/src/hooks/composer-branch-selection.tssrc/renderer/src/hooks/useComposerState.tssrc/renderer/src/i18n/locales/en.jsonsrc/renderer/src/i18n/locales/es.jsonsrc/renderer/src/i18n/locales/ja.jsonsrc/renderer/src/i18n/locales/ko.jsonsrc/renderer/src/i18n/locales/zh.jsonsrc/shared/composer-branch-selection.ts
| listWorktreesMock | ||
| .mockResolvedValueOnce([ | ||
| { | ||
| path: '/workspace/repo', |
There was a problem hiding this comment.
Use path.join(...) in test paths to keep cross-platform behavior deterministic.
These POSIX-only literals can break on Windows path separators; build expected paths with Node path helpers in mocks and assertions.
Suggested patch
+import path from 'node:path'
...
- path: '/workspace/repo',
+ path: path.join('/workspace', 'repo'),
...
- path: '/workspace/repo',
+ path: path.join('/workspace', 'repo'),
...
- path: '/workspace/my-folder',
+ path: path.join('/workspace', 'my-folder'),
...
- '/workspace/repo',
- '/workspace/my-folder',
+ path.join('/workspace', 'repo'),
+ path.join('/workspace', 'my-folder'),
...
- 'repo-1::/workspace/my-folder',
+ `repo-1::${path.join('/workspace', 'my-folder')}`,
...
- path: '/workspace/my-folder',
+ path: path.join('/workspace', 'my-folder'),As per coding guidelines: “Use path.join or Electron/Node path utilities for file paths — never assume / or \ for cross-platform compatibility.”
Also applies to: 1046-1046, 1053-1053, 1070-1071, 1079-1079, 1084-1084
Source: Coding guidelines
Screenshots — "Reuse this branch" in the new-worktree composerCaptured from a local Orca dev build of this branch (repo 1. Default — reuse on. Selecting an existing local branch shows the checkbox checked, so the branch is checked out instead of branching off it. 2. Opt out — reuse off. Unchecking creates a new branch from the selected ref as base (prior behavior). 3. The fix — reuse survives a folder rename. Worktree folder renamed to |
…ility) Address review feedback on the "Reuse branch" control: - Move the checkbox directly under the branch (smart) selection instead of the Advanced "Name" field — the worktree folder name can legitimately differ from the branch, so the choice belongs next to the branch pick and is now visible without expanding Advanced. - Rename the label "Reuse this branch" -> "Reuse branch". - Make eligibility dynamic: reuse is impossible when the branch is already checked out in another worktree (git allows a branch in only one worktree), so the checkbox is now hidden in that case (it was already hidden for remote-only refs). When a busy branch is picked, the override is no longer pinned to it, so creation cleanly falls back to a new branch from that ref as base instead of a silently-suffixed branch. Adds isBranchCheckedOutInWorktrees (pure, unit-tested) and threads the repo's worktree branch list from the store into the selection logic.
Updated per review feedbackCaptured from a fresh local dev build of the latest commit. (Supersedes the earlier screenshots — placement + label changed.) 1 + 2 — "Reuse branch" now sits directly under the branch selection (not under the Advanced "Name" field), and is renamed to "Reuse branch". It's visible without expanding Advanced. 3 — Dynamic: it's only shown when reuse is actually possible. Reuse is impossible when the branch is already checked out in another worktree (git allows a branch in only one worktree), so the checkbox is hidden there (it was already hidden for remote-only refs). Here Why under the branch, not the name: the worktree folder name is separate (in Advanced) and can differ from the branch. Reuse stays checked even after renaming the worktree to When a busy branch is picked, the branch override is no longer pinned to it, so creation cleanly falls back to a new branch from that ref as base instead of a silently-suffixed branch. |
Keep the "Reuse branch" row mounted and collapse it with a grid-rows transition (same pattern as the Advanced drawer) instead of conditionally rendering it, so the create-worktree dialog grows and shrinks smoothly when the option appears/disappears as the selected branch changes. The checkbox is taken out of the tab order while collapsed.
From a multi-dimensional review of the branch (all findings low severity): - Reset reuse state (reuseEligibleBranch, reuseSelectedBranch, branchNameOverridePreservesNameEdits) on repo and project switches, matching the other reset paths — avoids carrying stale branch-scoped state. - Disable the reuse checkbox while collapsed so no focusable control lives inside the aria-hidden row. - Extract the busy-branch override decision into a pure resolveComposerReuseOverride helper and unit-test it (busy local branch drops the override; remote-only ref keeps it) — pins the "no suffixed-branch collision" guarantee. - Note the worktreesByRepo visibility limitation near the busy-branch check (a branch busy only in a hidden external worktree is caught by the backend). - Strengthen tests: reuse-checkbox toggle in both directions, empty worktree list case.
|
@nwparker can we please extend this so that it will also appear on non-local branches (e.g origin/my-branch), and that when checked, will checkout the same branch name locally and set the target branch to the origin one? Currently if you create a new worktree from an existing remote-only branch, it will create a new (differently named) local branch and will NOT set the upstream target to the origin/* one. |
…ablyai#5181) (stablyai#5781) * feat(worktree): reuse an existing branch when creating a worktree (stablyai#5181) Adds an explicit "Reuse this branch" checkbox to the new-worktree composer. When an existing local branch is selected in the smart name field, the checkbox (default on) checks that branch out instead of creating a new branch from it — and the choice now survives renaming the worktree folder. Previously, reusing an existing branch only worked implicitly: it required keeping the auto-filled worktree name equal to the branch, and editing the name silently reverted to "create a new branch". There was no discoverable, durable control — the gap the issue reports. The fix is renderer-only: picking an existing local branch already sets baseBranch === branchNameOverride, so the existing backend reuse path (`git worktree add <path> <branch>`, preserveBranchOnDelete) checks the branch out correctly across local/SSH/runtime. The checkbox pins that override via the existing branchNameOverridePreservesNameEdits flag so the worktree folder can be named independently while the branch is reused; unchecking creates a fresh branch from the selected ref as base. - New pure helper resolveComposerBranchReuse (local-vs-remote + default). - Checkbox hidden for remote-only refs and non-branch sources. - Tests: pure helper, card render/toggle, and a backend integration test proving a renamed folder still reuses the exact branch. * fix(worktree): refine reuse-branch checkbox (placement, label, eligibility) Address review feedback on the "Reuse branch" control: - Move the checkbox directly under the branch (smart) selection instead of the Advanced "Name" field — the worktree folder name can legitimately differ from the branch, so the choice belongs next to the branch pick and is now visible without expanding Advanced. - Rename the label "Reuse this branch" -> "Reuse branch". - Make eligibility dynamic: reuse is impossible when the branch is already checked out in another worktree (git allows a branch in only one worktree), so the checkbox is now hidden in that case (it was already hidden for remote-only refs). When a busy branch is picked, the override is no longer pinned to it, so creation cleanly falls back to a new branch from that ref as base instead of a silently-suffixed branch. Adds isBranchCheckedOutInWorktrees (pure, unit-tested) and threads the repo's worktree branch list from the store into the selection logic. * feat(worktree): animate the reuse-branch row's show/hide Keep the "Reuse branch" row mounted and collapse it with a grid-rows transition (same pattern as the Advanced drawer) instead of conditionally rendering it, so the create-worktree dialog grows and shrinks smoothly when the option appears/disappears as the selected branch changes. The checkbox is taken out of the tab order while collapsed. * refactor(worktree): address review nits on reuse-branch control From a multi-dimensional review of the branch (all findings low severity): - Reset reuse state (reuseEligibleBranch, reuseSelectedBranch, branchNameOverridePreservesNameEdits) on repo and project switches, matching the other reset paths — avoids carrying stale branch-scoped state. - Disable the reuse checkbox while collapsed so no focusable control lives inside the aria-hidden row. - Extract the busy-branch override decision into a pure resolveComposerReuseOverride helper and unit-test it (busy local branch drops the override; remote-only ref keeps it) — pins the "no suffixed-branch collision" guarantee. - Note the worktreesByRepo visibility limitation near the busy-branch check (a branch busy only in a hidden external worktree is caught by the backend). - Strengthen tests: reuse-checkbox toggle in both directions, empty worktree list case.
|
Ah got it.. i need to sleect "branch" that UI / UX there is super strange.. will prepare issue for this |







Summary
Fixes #5181 — adds an explicit, discoverable way to reuse an existing branch when creating a worktree, instead of always creating a new branch from it.
When you select an existing local branch in the new‑worktree composer, a "Reuse branch" checkbox appears directly under the branch selection (defaults on). With it checked, Orca checks that branch out into the new worktree; unchecking it creates a fresh branch from the selected ref as base.
Why it's valid
A
checkoutExistingBranchpath shipped in #2543, but it was implicit: reuse only triggered when the auto‑filled worktree name stayed equal to the branch, and editing the worktree name silently reverted to "create a new branch" with no feedback. There was no discoverable control — the reporter filed this 3 weeks later saying it was "impossible." This PR makes it explicit and durable.Approach (renderer‑only)
Picking an existing local branch already sets
baseBranch === branchNameOverride === <branch>, so the existing backend reuse path (git worktree add <path> <branch>,preserveBranchOnDelete) checks the branch out across local, SSH, and runtime — no backend/IPC/schema changes. The checkbox pins that override via the existingbranchNameOverridePreservesNameEditsflag so reuse survives renaming the worktree folder (the folder name lives in Advanced and can differ from the branch).UX details
origin/…) and for a branch already checked out in another worktree (git allows a branch in only one worktree). When a busy branch is picked, the override isn't pinned, so creation cleanly falls back to a new branch from that ref.Verification
resolveComposerBranchReuse,isBranchCheckedOutInWorktrees,resolveComposerReuseOverride) unit‑tested; component render tests (checkbox shown/collapsed, both‑direction toggle); backend integration test (renamed folder still reuses →checkoutExistingBranch+preserveBranchOnDelete).disabled, override‑helper extraction + tests).