{"record":{"id":"8f2f27df8915fbb2","repo":"xai-org/grok-build","slug":"git-reset-hard-failed","errorCode":null,"errorMessage":"git reset --hard {} failed: {}","messagePattern":"git reset --hard (.+?) failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-fast-worktree/src/git/checkout.rs","lineNumber":42,"sourceCode":"    cmd.envs(xai_tty_utils::pager_env());\n    for &(key, val) in &GIT_AUTH_SUPPRESSION_ENVS {\n        cmd.env(key, val);\n    }\n    cmd.arg(\"--no-optional-locks\");\n    cmd\n}\n\n/// Run `git reset --hard <target>` (defaults to `HEAD`). Blocking.\npub(crate) fn git_reset_hard_command(worktree_path: &Path, target: Option<&str>) -> Result<()> {\n    let tgt = target.unwrap_or(\"HEAD\");\n    let output = git_command()\n        .current_dir(worktree_path)\n        .args([\"reset\", \"--hard\", tgt])\n        .output()\n        .context(\"failed to run git reset\")?;\n\n    if !output.status.success() {\n        anyhow::bail!(\n            \"git reset --hard {} failed: {}\",\n            tgt,\n            String::from_utf8_lossy(&output.stderr)\n        );\n    }\n\n    tracing::debug!(path = %worktree_path.display(), target = %tgt, \"git reset --hard\");\n    Ok(())\n}\n\n/// Run `git clean -fd` (or `-fdx`) to remove untracked files and directories.\n///\n/// When `include_ignored` is `true`, also removes files covered by `.gitignore`\n/// (equivalent to `git clean -fdx`). This is useful when recycling worktrees\n/// in a pool, where leftover build artifacts must be purged.\n///\n/// This is a blocking operation.\npub(crate) fn git_clean_fd(worktree_path: &Path, include_ignored: bool) -> Result<()> {","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-fast-worktree/src/git/checkout.rs#L24-L60","documentation":"`git_reset_hard_command` runs `git reset --hard <target>` inside the worktree; on a non-zero exit it bails with this message embedding the target ref and git's stderr. It means git itself rejected the reset — the worktree may be left in an inconsistent state.","triggerScenarios":"Calling sync/reset paths (`sync_worktree_opts`, `sync_from_precomputed`) with an invalid or unknown target ref, a corrupt repo, locked index, or a worktree whose git metadata is broken — anything making `git reset --hard` exit non-zero.","commonSituations":"Target commit doesn't exist (bad SHA, pruned branch); detached HEAD conflicts in a linked worktree; `.git/index.lock` left behind by a crashed process; shallow clones missing the target object; permission problems on .git.","solutions":["Read the git stderr in the error message — it names the actual cause (unknown revision, lock file, etc.).","Verify the target ref exists in the worktree: `git rev-parse --verify <tgt>`; fix the ref or fetch it.","Remove a stale lock: delete `.git/worktrees/<name>/index.lock` (or the repo's `index.lock`) if no git process is running.","Check the worktree's git linkage (`git worktree list`) and repair with `git worktree repair` if metadata is broken.","If objects are missing (shallow/partial clone), run `git fetch --unshallow` or fetch the specific commit, then retry the sync."],"exampleFix":"// before\nlet tgt = \"abc123\"; // commit not present in shallow clone\nreset_hard(worktree, tgt)?; // git reset --hard abc123 failed: unknown revision\n// after\n// ensure the object exists before resetting\nif !rev_parse_ok(worktree, tgt) {\n    run_in(worktree, &[\"git\", \"fetch\", \"origin\", tgt]);\n}\nreset_hard(worktree, tgt)?;\n","handlingStrategy":"validation","validationCode":"use std::process::Command;\nuse std::path::Path;\nfn ref_exists(worktree: &Path, tgt: &str) -> bool {\n    Command::new(\"git\").current_dir(worktree)\n        .args([\"rev-parse\", \"--verify\", \"--quiet\", &format!(\"{tgt}^{{commit}}\")])\n        .output().map(|o| o.status.success()).unwrap_or(false)\n}\nassert!(ref_exists(worktree_path, tgt), \"target ref missing in worktree\");\nassert!(!worktree_path.join(\".git\").join(\"index.lock\").exists(), \"stale index.lock\");","typeGuard":null,"tryCatchPattern":"match reset_hard(worktree, tgt) {\n    Err(e) if e.to_string().contains(\"git reset --hard\") => {\n        eprintln!(\"reset failed: {e}\"); // stderr embedded\n        // fetch missing objects / clear index.lock, then retry once\n    }\n    r => r?,\n}","preventionTips":["Verify the target ref resolves before resetting","Fetch required commits in shallow/partial clones","Clean up stale .git/index.lock files after crashed runs","Run `git worktree repair` if worktree metadata looks broken"],"tags":["git","reset","worktree","external-command"],"backgroundTag":"git-command-failed","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}