From 4b8f5a61a1552a0e3158ab631cf5460e29ad6128 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Sun, 8 Mar 2026 23:26:31 -0700 Subject: [PATCH] Prevent worktree creation on main branch --- src/git.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/git.ts b/src/git.ts index 77b4a57..6afb717 100644 --- a/src/git.ts +++ b/src/git.ts @@ -51,12 +51,16 @@ export async function createWorktree(branch: string, worktreePath: string, cwd: let result let switchedFromBranch = false if (exists === "local") { + const main = await mainBranch(cwd) + if (branch === main) { + throw new Error(`Cannot create a worktree for the main branch "${main}".`) + } // If the branch is checked out in the main worktree, switch it to main first if (await currentBranch(cwd) === branch) { if (await isDirty(cwd)) { throw new Error(`Cannot move branch "${branch}" to a worktree: the main worktree has uncommitted changes. Commit or stash them first.`) } - await checkout(await mainBranch(cwd), cwd) + await checkout(main, cwd) switchedFromBranch = true } result = await $`git worktree add ${worktreePath} ${branch}`.cwd(cwd).nothrow().quiet()