gastownhall/beads · error

failed to initialize git repository: %v %s

Error message

failed to initialize git repository: %v
%s

What it means

`bd init` attempts `git init` when the working directory is not already a git repository (and BEADS_DIR was not explicitly set). If the git command fails, bd wraps the error together with git's combined stdout/stderr output.

Source

Thrown at cmd/bd/init.go:1110

						fmt.Printf("  %s Removed leaked beads section from tracked .gitignore\n", ui.RenderPass("✓"))
					}
				} else if err := doctor.EnsureProjectGitignore(cwd); err != nil {
					fmt.Fprintf(os.Stderr, "Warning: failed to update project .gitignore: %v\n", err)
					// Non-fatal - continue anyway
				}
			}

		}

		// Ensure git is initialized — bd requires git for role config, sync branches,
		// hooks, worktrees, and fingerprint computation. git init is idempotent so
		// safe to call even if already in a git repo.
		// Skip when BEADS_DIR is explicitly set — the caller may be creating a
		// standalone .beads/ directory outside any git repo.
		if !isGitRepo() && !hasExplicitBeadsDir {
			gitInitCmd := exec.Command("git", "init")
			if output, err := gitInitCmd.CombinedOutput(); err != nil {
				return fmt.Errorf("failed to initialize git repository: %v\n%s", err, output)
			}
			// Clear cached git context so subsequent operations (e.g. hook
			// installation) see the newly-created repository (GH#2899).
			git.ResetCaches()
			if !quiet {
				fmt.Printf("  %s Initialized git repository\n", ui.RenderPass("✓"))
			}
		}

		// Ensure storage directory exists (.beads/dolt).
		// In server mode, dolt.New() connects via TCP and doesn't create local directories,
		// so we create the marker directory explicitly.
		// In embedded mode the engine creates its own directories under .beads/embeddeddolt/,
		// so skip this to avoid leaving an empty .beads/dolt/ artifact (GH#2903).
		if initServerMode {
			if err := os.MkdirAll(initDBPath, config.BeadsDirPerm); err != nil {
				return fmt.Errorf("failed to create storage directory %s: %v", initDBPath, err)
			}

View on GitHub (pinned to 71377f2769)

Solutions

  1. Install git or fix PATH so `git init` works: which git
  2. Check git's emitted output (second line of the error) for the real cause
  3. For 'detected dubious ownership', add: git config --global --add safe.directory <path>
  4. Skip auto-init by setting BEADS_DIR to create a standalone .beads directory

Example fix

// before
$ bd init  # fatal: detected dubious ownership in repository
// after
$ git config --global --add safe.directory /srv/project
$ bd init
Defensive patterns

Strategy: validation

Validate before calling

command -v git >/dev/null || { echo "git not installed"; exit 1; }
git rev-parse --git-dir >/dev/null 2>&1 || git init --dry-run >/dev/null || echo "git init would fail here"

Try / catch

bd init 2>init.log || { grep -A2 'failed to initialize git' init.log; git config --global --add safe.directory "$PWD"; }

Prevention

When it happens

Trigger: exec.Command("git", "init").CombinedOutput() returns a non-nil error during `bd init` in a non-git directory with no explicit BEADS_DIR.

Common situations: git is not installed or not on PATH, `git init` fails due to a permissions/ownership 'dubious ownership' safe.directory rejection, or a broken GIT_DIR/GIT_CONFIG environment in CI containers.

Related errors


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