gastownhall/beads · error
not in a git worktree: %w
Error message
not in a git worktree: %w
What it means
This error wraps a failure of `git rev-parse --show-toplevel` run in the current directory while building a worktree removal plan. It means bd could not determine the top-level of the current git worktree, so it cannot confirm the caller is inside a managed git worktree. bd throws it as a precondition for any worktree remove operation, since removal logic needs the current worktree root to resolve relative targets.
Source
Thrown at cmd/bd/worktree_cmd.go:1226
}
func prepareWorktreeRemoval(
ctx context.Context,
name string,
options *worktreeRemoveOptions,
afterTargetResolution func() error,
) (*worktreeRemovalPlan, error) {
gitRunner, err := newWorktreeRemovalGit()
if err != nil {
return nil, err
}
currentDirectory, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("failed to resolve current directory: %w", err)
}
currentRootOutput, err := gitRunner.output(ctx, currentDirectory, "rev-parse", "--show-toplevel")
if err != nil {
return nil, fmt.Errorf("not in a git worktree: %w", err)
}
currentRoot := filepath.Clean(strings.TrimSpace(string(currentRootOutput)))
worktrees, err := listRegisteredWorktrees(ctx, gitRunner, currentRoot)
if err != nil {
return nil, err
}
mainWorktree := worktrees[0]
if mainWorktree.bare {
return nil, fmt.Errorf("cannot remove a worktree when the primary worktree is bare")
}
targetEntry, err := resolveRegisteredWorktree(name, currentRoot, mainWorktree.path, worktrees)
if err != nil {
return nil, err
}
plan := &worktreeRemovalPlan{
force: options.force.value,
prepareFacts: worktreeremove.PrepareFacts{View on GitHub (pinned to 71377f2769)
Solutions
- cd into a directory inside a valid git worktree (or the repository root) and re-run the command
- Verify `git rev-parse --show-toplevel` succeeds manually in the current directory; fix the repo (e.g. restore .git) if it fails
- Check for and unset stale GIT_DIR / GIT_WORK_TREE / GIT_COMMON_DIR environment variables
- Confirm git is installed and on PATH, and that the directory is readable/executable
Example fix
// before: running from ~/notes (not a git repo) $ bd worktree remove feature-x // error: not in a git worktree: fatal: not a git repository... // after $ cd ~/src/myrepo && bd worktree remove feature-x
Defensive patterns
Strategy: validation
Validate before calling
if !test -d .git && ! git rev-parse --show-toplevel >/dev/null 2>&1; then echo 'not in a git worktree'; exit 1; fi
Type guard
func inGitWorktree(dir string) bool {
out, err := exec.Command("git", "-C", dir, "rev-parse", "--show-toplevel").Output()
return err == nil && len(bytes.TrimSpace(out)) > 0
} Try / catch
root, err := findWorktreeRoot(ctx)
if err != nil {
var wrapped *fmt.wrapError
if errors.As(err, &wrapped) && strings.Contains(wrapped.Unwrap().Error(), "not a git repository") {
return fmt.Errorf("run bd from inside a git worktree")
}
return err
} Prevention
- Always run bd worktree commands from inside a valid git worktree
- Sanity-check with `git rev-parse --show-toplevel` in scripts before invoking bd
- Unset GIT_DIR/GIT_WORK_TREE overrides in CI environments
- Keep git installed and updated on machines running bd
When it happens
Trigger: Calling `bd worktree remove` (via buildWorktreeRemovalPlan at cmd/bd/worktree_cmd.go:1226) from a directory where `git rev-parse --show-toplevel` fails: outside any repository, inside a corrupt .git, or with git unavailable/misbehaving in the given directory.
Common situations: Running bd in a non-git directory; a broken or deleted .git directory; GIT_DIR/GIT_WORK_TREE env vars pointing to invalid locations; running from a path with permission problems so git cannot traverse it; a too-old or missing git binary.
Related errors
- registered worktree not found: %s
- worktree name %q is ambiguous; use an absolute path (matches
- auto-export: git add failed: %w
- failed to read .gitignore: %w
- failed to write .gitignore: %w
AI-assisted analysis of gastownhall/beads@71377f2769 (2026-08-30).
Data as JSON: /api/errors/bab584b14262980e.
Report an issue: GitHub.