gastownhall/beads · error
target common git directory %q does not match repository %q
Error message
target common git directory %q does not match repository %q
What it means
During removal planning, bd compares the target worktree's common git directory with the primary worktree's common dir. A mismatch means the 'worktree' being removed does not actually belong to the same repository (its common dir resolves elsewhere), so removal would corrupt an unrelated repo. bd records the mismatch in plan facts (CommonDir = Unmatched) and returns the plan with this prepare error instead of proceeding.
Source
Thrown at cmd/bd/worktree_cmd.go:1304
status := worktreeremove.Clean
if target.status != "" {
status = worktreeremove.Dirty
}
plan.prepareFacts = worktreeremove.PrepareFacts{
Registration: worktreeremove.Present,
Target: worktreeremove.RegisteredTarget,
RegisteredPath: target.path,
TargetDir: worktreeremove.Present,
GitAdminDir: worktreeremove.Present,
GitMarker: worktreeremove.Present,
CommonDir: worktreeremove.Matched,
Head: worktreeremove.Present,
Status: status,
ManagedIgnore: worktreeremove.IgnoreAbsent,
}
if !sameWorktreePath(target.commonDir, mainCommonDir) {
plan.prepareFacts.CommonDir = worktreeremove.Unmatched
plan.prepareErr = fmt.Errorf(
"target common git directory %q does not match repository %q",
target.commonDir,
mainCommonDir,
)
return plan, nil
}
if relative, inside := relativeWorktreePath(plan.mainWorktree, target.path); inside {
plan.gitignoreCleanup, err = prepareGitignoreCleanup(plan.mainWorktree, relative)
if err != nil {
return nil, fmt.Errorf("cannot safely prepare .gitignore cleanup: %w", err)
}
if plan.gitignoreCleanup != nil {
plan.prepareFacts.ManagedIgnore = worktreeremove.IgnoreManaged
plan.prepareFacts.ManagedIgnoreEntry = plan.gitignoreCleanup.entry
}
}
if options.force.value {View on GitHub (pinned to 71377f2769)
Solutions
- Verify the target is the intended repo: run `git -C <target> rev-parse --git-common-dir` and compare with the main repo
- Re-register/remove the stale worktree entry: `git worktree prune`, then `git worktree add` a fresh worktree of the correct repo and retry
- Remove the foreign worktree with plain `git worktree remove` inside the repo it actually belongs to
- Do not delete directories by hand; let git manage worktree metadata to keep common dirs consistent
Example fix
// before: /wt/feature is a worktree of a different clone $ bd worktree remove feature // error: target common git directory "/other/clone/.git" does not match repository "/main/.git" // after $ git worktree prune && git worktree add /wt/feature feature-branch $ bd worktree remove feature
Defensive patterns
Strategy: validation
Validate before calling
main=$(git rev-parse --path-format=absolute --git-common-dir)
tgt=$(git -C /wt/feature rev-parse --path-format=absolute --git-common-dir)
[ "$main" = "$tgt" ] || { echo 'target belongs to a different repository'; exit 1; } Type guard
func sameRepository(mainCommonDir string, target worktreeTarget) bool {
return filepath.Clean(target.commonDir) == filepath.Clean(mainCommonDir)
} Try / catch
plan, err := buildWorktreeRemovalPlan(ctx, git, name, nil)
if plan != nil && plan.prepareErr != nil && strings.Contains(plan.prepareErr.Error(), "does not match repository") {
return fmt.Errorf("re-add the worktree with `git worktree add` from the correct repo first")
} Prevention
- Create worktrees only via `git worktree add` from the primary repo
- Never copy worktree directories between clones
- Prune stale registrations with `git worktree prune` after manual deletions
- Check `git worktree list` paths match expectations before scripted removals
When it happens
Trigger: `bd worktree remove <name>` where `inspectWorktreeTarget` on the target yields a commonDir that does not path-match the main worktree's common dir — e.g. the registered path now hosts a worktree of a different repository, or GIT_COMMON_DIR/git dir files point across repos.
Common situations: A directory reused for another clone after the original worktree was deleted; manually copied worktree folders retaining a .git file pointing to the old repo; nested repos confusing resolution.
Related errors
- target common git directory changed
- failed to inspect created worktree cleanliness: %w %s
- created worktree is dirty after checkout; refusing to contin
- worktree not found: %s
- failed to read git worktree registry: %w
AI-assisted analysis of gastownhall/beads@71377f2769 (2026-08-30).
Data as JSON: /api/errors/4957c687d5a64f1c.
Report an issue: GitHub.