Skip to content

Commit 9525910

Browse files
Improve init-worktree script to handle existing branches gracefully
- Script now detects existing branches and creates worktrees from them - No longer fails when branch already exists - Cleaner command building logic for git worktree add - Better user messaging about what's happening 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent 84220af commit 9525910

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

scripts/init-worktree.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,10 @@ async function main(): Promise<void> {
262262
}
263263

264264
// Check if git branch already exists
265-
if (await checkGitBranchExists(args.name)) {
266-
throw new WorktreeError(
267-
`Git branch '${args.name}' already exists\nChoose a different worktree name or delete the existing branch`,
268-
'BRANCH_EXISTS'
265+
const branchExists = await checkGitBranchExists(args.name)
266+
if (branchExists) {
267+
console.log(
268+
`Git branch '${args.name}' already exists - will create worktree from existing branch`
269269
)
270270
}
271271

@@ -278,8 +278,14 @@ async function main(): Promise<void> {
278278
console.log(`Creating git worktree: ${args.name}`)
279279
console.log(`Location: ${worktreePath}`)
280280

281-
// Create the git worktree
282-
await runCommand('git', ['worktree', 'add', worktreePath, '-b', args.name])
281+
// Create the git worktree (with or without creating new branch)
282+
const worktreeAddArgs = ['worktree', 'add', worktreePath]
283+
if (branchExists) {
284+
worktreeAddArgs.push(args.name)
285+
} else {
286+
worktreeAddArgs.push('-b', args.name)
287+
}
288+
await runCommand('git', worktreeAddArgs)
283289

284290
console.log('Setting up worktree environment...')
285291
console.log(`Backend port: ${args.backendPort}`)

0 commit comments

Comments
 (0)