gastownhall/beads · error

failed to resolve repository common git directory: %w

Error message

failed to resolve repository common git directory: %w

What it means

bd failed to run `git rev-parse --path-format=absolute --git-common-dir` from the primary worktree, which is needed to learn the repository's shared/common git directory before planning the removal. Without the common dir, bd cannot verify that the target worktree belongs to the same repository. The underlying git error is wrapped via %w.

Source

Thrown at cmd/bd/worktree_cmd.go:1270

	if sameWorktreePath(targetEntry.path, currentRoot) {
		plan.prepareFacts.Target = worktreeremove.CurrentWorktree
		return plan, nil
	}
	if afterTargetResolution != nil {
		if err := afterTargetResolution(); err != nil {
			return nil, fmt.Errorf("worktree removal interrupted after target resolution: %w", err)
		}
	}

	mainCommonDirOutput, err := gitRunner.output(
		ctx,
		mainWorktree.path,
		"rev-parse",
		"--path-format=absolute",
		"--git-common-dir",
	)
	if err != nil {
		return nil, fmt.Errorf("failed to resolve repository common git directory: %w", err)
	}
	mainCommonDir := filepath.Clean(strings.TrimSpace(string(mainCommonDirOutput)))

	target, err := inspectWorktreeTarget(ctx, gitRunner, targetEntry)
	if err != nil {
		return nil, err
	}
	plan = &worktreeRemovalPlan{
		git:           gitRunner,
		executionRoot: filepath.Clean(mainWorktree.path),
		mainWorktree:  filepath.Clean(mainWorktree.path),
		commonDir:     mainCommonDir,
		target:        target,
		force:         options.force.value,
	}
	status := worktreeremove.Clean
	if target.status != "" {
		status = worktreeremove.Dirty

View on GitHub (pinned to 71377f2769)

Solutions

  1. Upgrade git to >= 2.31 which supports `--path-format=absolute`
  2. Run `git rev-parse --path-format=absolute --git-common-dir` in the primary worktree manually and fix the reported git error (repair .git, permissions)
  3. Remove any GIT_DIR/GIT_COMMON_DIR env overrides that confuse rev-parse
  4. Restore or re-clone the repository if its .git structure is corrupt

Example fix

// before: git 2.25
$ bd worktree remove feature-x
// error: failed to resolve repository common git directory: unknown option: --path-format
// after
$ brew upgrade git  # or apt install newer git (>= 2.31)
$ bd worktree remove feature-x
Defensive patterns

Strategy: validation

Validate before calling

git rev-parse --path-format=absolute --git-common-dir >/dev/null 2>&1 || { echo 'git too old or repo broken'; exit 1; }

Type guard

func supportsPathFormat() bool {
	return exec.Command("git", "rev-parse", "--path-format=absolute", "--git-common-dir").Run() == nil
}

Try / catch

if err != nil && strings.Contains(err.Error(), "common git directory") {
	if !isGitVersionAtLeast(2, 31) {
		return fmt.Errorf("upgrade git to >= 2.31")
	}
	return err
}

Prevention

When it happens

Trigger: Calling `bd worktree remove` when `git rev-parse --git-common-dir` fails in the primary worktree path: corrupted primary .git file/directory, unsupported git version lacking --path-format, or the primary worktree directory being unreadable.

Common situations: Very old git (< 2.31) that does not support --path-format=absolute; a deleted or corrupt .git in the main worktree; permission issues on the main worktree path; GIT_DIR overrides breaking resolution.

Related errors


AI-assisted analysis of gastownhall/beads@71377f2769 (2026-08-30). Data as JSON: /api/errors/5c5983f2959a708d. Report an issue: GitHub.