{"record":{"id":"caba0ccf3c5b0f2a","repo":"gastownhall/beads","slug":"cannot-remove-a-worktree-when-the-primary-worktree","errorCode":null,"errorMessage":"cannot remove a worktree when the primary worktree is bare","messagePattern":"cannot remove a worktree when the primary worktree is bare","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/bd/worktree_cmd.go","lineNumber":1236,"sourceCode":"\t\treturn nil, err\n\t}\n\tcurrentDirectory, err := os.Getwd()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to resolve current directory: %w\", err)\n\t}\n\tcurrentRootOutput, err := gitRunner.output(ctx, currentDirectory, \"rev-parse\", \"--show-toplevel\")\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"not in a git worktree: %w\", err)\n\t}\n\tcurrentRoot := filepath.Clean(strings.TrimSpace(string(currentRootOutput)))\n\n\tworktrees, err := listRegisteredWorktrees(ctx, gitRunner, currentRoot)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tmainWorktree := worktrees[0]\n\tif mainWorktree.bare {\n\t\treturn nil, fmt.Errorf(\"cannot remove a worktree when the primary worktree is bare\")\n\t}\n\ttargetEntry, err := resolveRegisteredWorktree(name, currentRoot, mainWorktree.path, worktrees)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tplan := &worktreeRemovalPlan{\n\t\tforce: options.force.value,\n\t\tprepareFacts: worktreeremove.PrepareFacts{\n\t\t\tRegistration: worktreeremove.Present,\n\t\t},\n\t}\n\tif targetEntry.isMain {\n\t\tplan.prepareFacts.Target = worktreeremove.PrimaryWorktree\n\t\treturn plan, nil\n\t}\n\tif sameWorktreePath(targetEntry.path, currentRoot) {\n\t\tplan.prepareFacts.Target = worktreeremove.CurrentWorktree\n\t\treturn plan, nil","sourceCodeStart":1218,"sourceCodeEnd":1254,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/cmd/bd/worktree_cmd.go#L1218-L1254","documentation":"bd refuses to remove a worktree when the primary (first registered) worktree is a bare repository. In a bare primary setup, worktree bookkeeping and the .gitignore/paths bd manages differ enough that the removal plan would be unsafe, so bd fails fast with this explicit error. It is thrown from the removal-plan builder (cmd/bd/worktree_cmd.go:1236).","triggerScenarios":"Invoking `bd worktree remove <name>` while the repository's primary worktree (worktrees[0] from `git worktree list`) has the `bare` flag, e.g. a repo created with `git init --bare` or `git clone --bare` with linked worktrees attached.","commonSituations":"Server-style bare repos with linked checkouts; CI setups cloning with --bare then adding worktrees; repos restructured so the bare dir became the primary worktree.","solutions":["Run the removal from / target a non-bare setup: create a normal primary worktree with `git worktree add <path> <branch>` (or re-clone non-bare) and remove the worktree there","Remove the worktree manually with `git worktree remove <path>` if bd's managed cleanup (.gitignore entries) is not needed","Restructure the repo so the bare directory is not the primary worktree (clone non-bare, add the bare as secondary or drop it)"],"exampleFix":"// before\n$ git init --bare /srv/repo.git && git worktree add /srv/main\n$ bd worktree remove /srv/main\n// error: cannot remove a worktree when the primary worktree is bare\n// after: use a non-bare primary\n$ git clone /srv/repo.git /srv/repo && cd /srv/repo && git worktree remove ../main","handlingStrategy":"validation","validationCode":"first=$(git worktree list --porcelain | awk '/^worktree /{print $2; exit}')\nif git -C \"$first\" rev-parse --is-bare-repository | grep -q true; then echo 'primary worktree is bare'; exit 1; fi","typeGuard":"func primaryWorktreeIsBare(entries []worktreeEntry) bool {\n\treturn len(entries) > 0 && entries[0].bare\n}","tryCatchPattern":"plan, err := buildWorktreeRemovalPlan(ctx, git, name, nil)\nif err != nil && strings.Contains(err.Error(), \"primary worktree is bare\") {\n\treturn fmt.Errorf(\"use `git worktree remove` directly, or set up a non-bare primary worktree\")\n}","preventionTips":["Avoid `git init --bare`/`clone --bare` for repos you manage worktrees in with bd","Clone with a normal working tree and add linked worktrees from it","If you need a bare server repo, perform worktree admin tasks in a non-bare clone","Check `git worktree list` for the bare flag before automating removals"],"tags":["git","worktree","bare-repository"],"backgroundTag":"bare-repository-unsupported","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}