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()