{"record":{"id":"4933b34de0e873d7","repo":"Hmbown/CodeWhale","slug":"git-worktree-add-failed-for-branch-at","errorCode":null,"errorMessage":"git worktree add failed for branch {} at {}{}{}","messagePattern":"git worktree add failed for branch (.+?) at (.+?)(.+?)(.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/lane/src/worktree.rs","lineNumber":60,"sourceCode":"    let base = spec.base_ref.as_deref().unwrap_or(\"HEAD\");\n    // Capture git output instead of inheriting the caller's terminal. Runtime\n    // callers include the raw-mode TUI launch screen, where even one inherited\n    // progress/error line corrupts the alternate-screen buffer.\n    let output = Command::new(\"git\")\n        .current_dir(&spec.repo_root)\n        .args([\n            \"worktree\",\n            \"add\",\n            \"-b\",\n            &spec.branch,\n            &spec.path.to_string_lossy(),\n            base,\n        ])\n        .output()\n        .context(\"git worktree add\")?;\n    if !output.status.success() {\n        let detail = String::from_utf8_lossy(&output.stderr).trim().to_string();\n        bail!(\n            \"git worktree add failed for branch {} at {}{}{}\",\n            spec.branch,\n            spec.path.display(),\n            if detail.is_empty() { \"\" } else { \": \" },\n            detail\n        );\n    }\n    Ok(ProvisionedWorktree {\n        path: spec.path.clone(),\n        branch: spec.branch.clone(),\n    })\n}\n\n/// Remove a worktree when TTL has expired (or immediately when TTL is 0).\n///\n/// `stopped_at` is RFC3339. When `ttl_secs` is `None`, no cleanup is performed.\n///\n/// Removal only ever touches a path that git identifies as a managed worktree","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/lane/src/worktree.rs#L42-L78","documentation":"Raised by `provision_worktree` (crates/lane/src/worktree.rs) when the `git worktree add` subprocess exits non-zero. The library captures git's stderr and appends it to the message: \"git worktree add failed for branch {branch} at {path}: {detail}\".","triggerScenarios":"`git worktree add -b <branch> <path> <base>` fails because the branch already exists, the target path already exists and is non-empty, the base ref is unknown, or the repo is bare/corrupt.","commonSituations":"Re-running provisioning after a partial previous run left the branch behind; reusing a lane name whose old branch was not yet cleaned; pointing base_ref at a ref that was rebased away; path collision with an existing checkout.","solutions":["Read the appended git stderr in the message — it names the exact git cause (e.g. 'already exists').","Delete or pick a new branch name if it already exists (`git branch -D <branch>`), or clean the stale worktree path (`git worktree prune`).","Verify base_ref exists (`git rev-parse --verify <base>`); use HEAD or a valid ref.","Ensure the target path does not already contain files before provisioning."],"exampleFix":"// before: reusing a branch from a previous run\nprovision_worktree(&spec)?; // branch 'lane/a' still exists\n// after\ncleanup_worktree(&spec)?; // or: git branch -D lane/a\nprovision_worktree(&spec)?;","handlingStrategy":"try-catch","validationCode":"// check branch/path/base before calling\nlet branch_taken = Command::new(\"git\").args([\"rev-parse\", \"--verify\", &spec.branch]).output()?.status.success();\nanyhow::ensure!(!branch_taken, \"branch {} already exists\", spec.branch);\nanyhow::ensure!(!spec.path.exists(), \"worktree path {} exists\", spec.path.display());","typeGuard":null,"tryCatchPattern":"match provision_worktree(&spec) {\n    Err(e) if e.to_string().contains(\"git worktree add failed\") => {\n        // run `git worktree prune`, delete stale branch, retry once\n    }\n    other => other?,\n}","preventionTips":["Run cleanup (git worktree prune, branch delete) before re-provisioning the same lane name.","Verify base_ref resolves before provisioning.","Check the target path is free before creating a worktree there."],"tags":["worktree","git","subprocess"],"backgroundTag":"git-command-failed","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}