{"record":{"id":"b43ca9299277f01f","repo":"zed-industries/zed","slug":"git-worktree-add-failed-stderr","errorCode":null,"errorMessage":"git worktree add failed: {stderr}","messagePattern":"git worktree add failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/git/src/repository.rs","lineNumber":2230,"sourceCode":"                        &stdout,\n                        main_worktree_path.as_deref(),\n                    ))\n                } else {\n                    let stderr = String::from_utf8_lossy(&output.stderr);\n                    anyhow::bail!(\"git worktree list failed: {stderr}\");\n                }\n            })\n            .boxed()\n    }\n\n    fn worktree_created_at(\n        &self,\n        worktree_path: PathBuf,\n    ) -> BoxFuture<'_, Result<Option<SystemTime>>> {\n        self.executor\n            .spawn(async move {\n                match std::fs::metadata(&worktree_path) {\n                    Err(error) if error.kind() == std::io::ErrorKind::NotFound => {\n                        return Ok(None);\n                    }\n                    Err(error) => {\n                        return Err(error).with_context(|| {\n                            format!(\"failed to stat {}\", worktree_path.display())\n                        });\n                    }\n                    Ok(_) => {}\n                }\n                let git_dir = linked_worktree_git_dir(&worktree_path)?;\n                let metadata = std::fs::metadata(&git_dir)\n                    .with_context(|| format!(\"failed to stat {}\", git_dir.display()))?;\n                let created_at = metadata.created().with_context(|| {\n                    format!(\"creation time unavailable for {}\", git_dir.display())\n                })?;\n                Ok(Some(created_at))\n            })\n            .boxed()","sourceCodeStart":2212,"sourceCodeEnd":2248,"githubUrl":"https://github.com/zed-industries/zed/blob/5a9b9558db01a6b906cec2fb70a797affdc58cdd/crates/git/src/repository.rs#L2212-L2248","documentation":"Creates a linked worktree with `git worktree add` (after creating the parent directory); a non-zero exit wraps git's stderr. Every git worktree-add failure mode appears here, and git's message names the precise problem.","triggerScenarios":"`fatal: '<branch>' is already checked out at '<path>'` (same branch in two worktrees); `fatal: '<path>' already exists and is not an empty directory` (leftover from a previous attempt); invalid branch name; a start point (commit-ish) that does not resolve; permission denied creating the directory or the worktree.","commonSituations":"Opening the same branch in a second worktree; retrying worktree creation after a partial failure left files behind; passing a branch that exists only on an unfetched remote; CI sandboxes denying writes to the parent directory.","solutions":["Read the appended stderr: for 'already checked out', pick another branch or reuse the existing worktree path.","Clean the leftover target directory (or pass force) so `worktree add` finds no non-empty path.","Verify the start point resolves: `git rev-parse --verify <start-point>`, and fetch if it only exists on the remote.","Check write permission on the parent directory that was just created."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// before add: target must be absent or empty, and the branch must not be checked out elsewhere\nif path.exists() && std::fs::read_dir(&path)?.next().is_some() {\n    anyhow::bail!(\"worktree path {path:?} already exists and is not empty\");\n}\nlet checked_out = git\n    .run(&[\"worktree\", \"list\", \"--porcelain\"])\n    .await?\n    .lines()\n    .filter(|l| l.starts_with(\"branch \"))\n    .any(|l| l.ends_with(&format!(\"refs/heads/{branch}\")));\nanyhow::ensure!(!checked_out, \"branch {branch} is already checked out in another worktree\");","typeGuard":null,"tryCatchPattern":"match repo.add_worktree(path, branch, None, false).await {\n    Err(e) if e.to_string().contains(\"already checked out\") => {\n        // pick a different branch, or point the user at the existing worktree\n    }\n    Err(e) if e.to_string().contains(\"already exists\") => {\n        // clean leftover directory or retry with force\n    }\n    other => other?,\n}","preventionTips":["Check the target directory is empty before creating a worktree.","Never check the same branch out in two worktrees; pick unique branches per worktree.","Verify the start point resolves (`git rev-parse --verify`) before calling add."],"tags":["git","worktree","subprocess","stderr"],"backgroundTag":"git-command-failed","analyzedSha":"5a9b9558db01a6b906cec2fb70a797affdc58cdd","analyzedAt":"2026-08-20T19:29:52.058Z","contentChangedAt":"2026-08-20T19:29:52.058Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}