{"record":{"id":"03d66df6c722fa9a","repo":"gastownhall/beads","slug":"failed-to-initialize-git-repository-v-s","errorCode":null,"errorMessage":"failed to initialize git repository: %v\n%s","messagePattern":"failed to initialize git repository: (.+?)\n(.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/bd/init.go","lineNumber":1110,"sourceCode":"\t\t\t\t\t\tfmt.Printf(\"  %s Removed leaked beads section from tracked .gitignore\\n\", ui.RenderPass(\"✓\"))\n\t\t\t\t\t}\n\t\t\t\t} else if err := doctor.EnsureProjectGitignore(cwd); err != nil {\n\t\t\t\t\tfmt.Fprintf(os.Stderr, \"Warning: failed to update project .gitignore: %v\\n\", err)\n\t\t\t\t\t// Non-fatal - continue anyway\n\t\t\t\t}\n\t\t\t}\n\n\t\t}\n\n\t\t// Ensure git is initialized — bd requires git for role config, sync branches,\n\t\t// hooks, worktrees, and fingerprint computation. git init is idempotent so\n\t\t// safe to call even if already in a git repo.\n\t\t// Skip when BEADS_DIR is explicitly set — the caller may be creating a\n\t\t// standalone .beads/ directory outside any git repo.\n\t\tif !isGitRepo() && !hasExplicitBeadsDir {\n\t\t\tgitInitCmd := exec.Command(\"git\", \"init\")\n\t\t\tif output, err := gitInitCmd.CombinedOutput(); err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to initialize git repository: %v\\n%s\", err, output)\n\t\t\t}\n\t\t\t// Clear cached git context so subsequent operations (e.g. hook\n\t\t\t// installation) see the newly-created repository (GH#2899).\n\t\t\tgit.ResetCaches()\n\t\t\tif !quiet {\n\t\t\t\tfmt.Printf(\"  %s Initialized git repository\\n\", ui.RenderPass(\"✓\"))\n\t\t\t}\n\t\t}\n\n\t\t// Ensure storage directory exists (.beads/dolt).\n\t\t// In server mode, dolt.New() connects via TCP and doesn't create local directories,\n\t\t// so we create the marker directory explicitly.\n\t\t// In embedded mode the engine creates its own directories under .beads/embeddeddolt/,\n\t\t// so skip this to avoid leaving an empty .beads/dolt/ artifact (GH#2903).\n\t\tif initServerMode {\n\t\t\tif err := os.MkdirAll(initDBPath, config.BeadsDirPerm); err != nil {\n\t\t\t\treturn fmt.Errorf(\"failed to create storage directory %s: %v\", initDBPath, err)\n\t\t\t}","sourceCodeStart":1092,"sourceCodeEnd":1128,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/cmd/bd/init.go#L1092-L1128","documentation":"`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.","triggerScenarios":"exec.Command(\"git\", \"init\").CombinedOutput() returns a non-nil error during `bd init` in a non-git directory with no explicit BEADS_DIR.","commonSituations":"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.","solutions":["Install git or fix PATH so `git init` works: which git","Check git's emitted output (second line of the error) for the real cause","For 'detected dubious ownership', add: git config --global --add safe.directory <path>","Skip auto-init by setting BEADS_DIR to create a standalone .beads directory"],"exampleFix":"// before\n$ bd init  # fatal: detected dubious ownership in repository\n// after\n$ git config --global --add safe.directory /srv/project\n$ bd init","handlingStrategy":"validation","validationCode":"command -v git >/dev/null || { echo \"git not installed\"; exit 1; }\ngit rev-parse --git-dir >/dev/null 2>&1 || git init --dry-run >/dev/null || echo \"git init would fail here\"","typeGuard":null,"tryCatchPattern":"bd init 2>init.log || { grep -A2 'failed to initialize git' init.log; git config --global --add safe.directory \"$PWD\"; }","preventionTips":["Install git and keep it on PATH","Add project paths to git safe.directory when ownership differs","Set BEADS_DIR explicitly for standalone (non-git) bead stores","Read the git output appended to the error for the root cause"],"tags":["git","init","subprocess"],"backgroundTag":"git-command-failed","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}