gastownhall/beads · error

failed to resolve target git directory: %w

Error message

failed to resolve target git directory: %w

What it means

inspectWorktreeTarget runs `git rev-parse --absolute-git-dir` inside the target worktree to pin its git directory. If that command fails, this wrapped error is returned. It means git could not determine the .git directory for the worktree — the worktree is broken, missing, or git itself errored.

Source

Thrown at cmd/bd/worktree_cmd.go:957

	detached             bool
	bare                 bool
	locked               bool
	lockReason           string
	prunable             bool
	pruneReason          string
	status               string
	statusFingerprint    string
	registryID           string
}

func inspectWorktreeTarget(
	ctx context.Context,
	git *worktreeRemovalGit,
	worktree registeredWorktree,
) (pinnedWorktreeTarget, error) {
	gitDirOutput, err := git.output(ctx, worktree.path, "rev-parse", "--absolute-git-dir")
	if err != nil {
		return pinnedWorktreeTarget{}, fmt.Errorf("failed to resolve target git directory: %w", err)
	}
	gitDir := filepath.Clean(strings.TrimSpace(string(gitDirOutput)))
	commonDirOutput, err := git.output(
		ctx,
		worktree.path,
		"rev-parse",
		"--path-format=absolute",
		"--git-common-dir",
	)
	if err != nil {
		return pinnedWorktreeTarget{}, fmt.Errorf("failed to resolve target common git directory: %w", err)
	}
	headOutput, err := git.output(
		ctx,
		worktree.path,
		"rev-parse",
		"--verify",
		"--quiet",

View on GitHub (pinned to 71377f2769)

Solutions

  1. Inspect the wrapped git error; reproduce with `git -C <path> rev-parse --absolute-git-dir` to see the raw failure.
  2. If the directory is gone, run `git worktree prune` and clean the bd registry entry, then retry.
  3. If the .git file's gitdir pointer dangles (repo was moved), recreate the worktree with `git worktree add` and re-register it.
  4. Verify git is on PATH and the path is readable by the current user.

Example fix

// before
bd worktree remove feature-x
// error: failed to resolve target git directory: ... (directory deleted)
// after
git worktree prune
bd worktree remove feature-x   # registry now clean
Defensive patterns

Strategy: try-catch

Validate before calling

const probe = Bun.spawnSync(["git", "-C", worktreePath, "rev-parse", "--absolute-git-dir"]);
if (probe.exitCode !== 0) {
  throw new Error(`worktree broken, prune instead: ${probe.stderr.toString()}`);
}

Try / catch

try {
  await bd("worktree", "remove", path);
} catch (e) {
  if (String(e.message).includes("failed to resolve target git directory")) {
    execSync("git worktree prune");
  } else throw e;
}

Prevention

When it happens

Trigger: Removal or revalidation on a registered worktree whose directory was deleted or corrupted, so git cannot resolve its git dir; git missing from PATH; the worktree's `.git` file points to a nonexistent gitdir; context cancellation kills the git subprocess.

Common situations: Someone deleted the worktree with rm -rf but the registry still lists it; a network-mounted worktree became unreadable; the main repo was moved so the .git file's gitdir pointer dangles; interrupted worktree creation.

Related errors


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