{"record":{"id":"466c1c80c01cb85a","repo":"gastownhall/beads","slug":"failed-to-resolve-target-common-git-directory-w","errorCode":null,"errorMessage":"failed to resolve target common git directory: %w","messagePattern":"failed to resolve target common git directory: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/bd/worktree_cmd.go","lineNumber":968,"sourceCode":"func inspectWorktreeTarget(\n\tctx context.Context,\n\tgit *worktreeRemovalGit,\n\tworktree registeredWorktree,\n) (pinnedWorktreeTarget, error) {\n\tgitDirOutput, err := git.output(ctx, worktree.path, \"rev-parse\", \"--absolute-git-dir\")\n\tif err != nil {\n\t\treturn pinnedWorktreeTarget{}, fmt.Errorf(\"failed to resolve target git directory: %w\", err)\n\t}\n\tgitDir := filepath.Clean(strings.TrimSpace(string(gitDirOutput)))\n\tcommonDirOutput, err := git.output(\n\t\tctx,\n\t\tworktree.path,\n\t\t\"rev-parse\",\n\t\t\"--path-format=absolute\",\n\t\t\"--git-common-dir\",\n\t)\n\tif err != nil {\n\t\treturn pinnedWorktreeTarget{}, fmt.Errorf(\"failed to resolve target common git directory: %w\", err)\n\t}\n\theadOutput, err := git.output(\n\t\tctx,\n\t\tworktree.path,\n\t\t\"rev-parse\",\n\t\t\"--verify\",\n\t\t\"--quiet\",\n\t\t\"--end-of-options\",\n\t\t\"HEAD^{commit}\",\n\t)\n\tif err != nil {\n\t\treturn pinnedWorktreeTarget{}, fmt.Errorf(\"target HEAD does not resolve to a commit: %w\", err)\n\t}\n\tstatusOutput, err := git.output(\n\t\tctx,\n\t\tworktree.path,\n\t\t\"status\",\n\t\t\"--porcelain=v1\",","sourceCodeStart":950,"sourceCodeEnd":986,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/cmd/bd/worktree_cmd.go#L950-L986","documentation":"After resolving the per-worktree git dir, inspectWorktreeTarget runs `git rev-parse --path-format=absolute --git-common-dir` to find the repository's shared common git directory. Failure is wrapped in this error: git could not identify the shared objects/refs directory backing the worktree.","triggerScenarios":"The worktree's linkage to its parent repository is broken (dangling gitdir pointer, moved or deleted main repo .git), or the git subprocess fails or is canceled; also git too old to support --path-format=absolute.","commonSituations":"The main repository was moved, renamed, or deleted after worktree creation; a worktree copied to another machine without its main repo; git < 2.31; context timeout during removal.","solutions":["Check the wrapped git error; reproduce with `git -C <path> rev-parse --git-common-dir`.","Verify the main repository still exists at the location in the worktree's .git file; restore or re-clone it if moved.","Recreate the broken worktree: `git worktree remove --force <path>` (or prune) then `git worktree add` again.","Upgrade git to >= 2.31 if the flag itself is unsupported."],"exampleFix":"// before\nbd worktree remove feature-x\n// error: failed to resolve target common git directory: ... (main .git moved)\n// after\ngit worktree prune\nrm -rf /repo/.worktrees/feature-x\ngit worktree add /repo/.worktrees/feature-x main","handlingStrategy":"try-catch","validationCode":"const probe = Bun.spawnSync([\"git\", \"-C\", worktreePath, \"rev-parse\", \"--git-common-dir\"]);\nif (probe.exitCode !== 0) {\n  throw new Error(\"common dir unresolvable; main repo missing or moved\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  await bd(\"worktree\", \"remove\", path);\n} catch (e) {\n  if (String(e.message).includes(\"failed to resolve target common git directory\")) {\n    execSync(`git worktree prune && git worktree add ${path} main`);\n  } else throw e;\n}","preventionTips":["Never move or rename the main repository that owns worktrees.","Copy whole repos (main + worktrees) when migrating machines.","Require git >= 2.31 for --path-format=absolute.","Quiesce concurrent git operations before removal."],"tags":["worktree","git","rev-parse","broken-worktree"],"backgroundTag":"broken-git-worktree","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}