xai-org/grok-build · error
git reset --hard {} failed: {}
Error message
git reset --hard {} failed: {} What it means
`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.
Source
Thrown at crates/codegen/xai-fast-worktree/src/git/checkout.rs:42
cmd.envs(xai_tty_utils::pager_env());
for &(key, val) in &GIT_AUTH_SUPPRESSION_ENVS {
cmd.env(key, val);
}
cmd.arg("--no-optional-locks");
cmd
}
/// Run `git reset --hard <target>` (defaults to `HEAD`). Blocking.
pub(crate) fn git_reset_hard_command(worktree_path: &Path, target: Option<&str>) -> Result<()> {
let tgt = target.unwrap_or("HEAD");
let output = git_command()
.current_dir(worktree_path)
.args(["reset", "--hard", tgt])
.output()
.context("failed to run git reset")?;
if !output.status.success() {
anyhow::bail!(
"git reset --hard {} failed: {}",
tgt,
String::from_utf8_lossy(&output.stderr)
);
}
tracing::debug!(path = %worktree_path.display(), target = %tgt, "git reset --hard");
Ok(())
}
/// Run `git clean -fd` (or `-fdx`) to remove untracked files and directories.
///
/// When `include_ignored` is `true`, also removes files covered by `.gitignore`
/// (equivalent to `git clean -fdx`). This is useful when recycling worktrees
/// in a pool, where leftover build artifacts must be purged.
///
/// This is a blocking operation.
pub(crate) fn git_clean_fd(worktree_path: &Path, include_ignored: bool) -> Result<()> {View on GitHub (pinned to bc7f02eddd)
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.
Example fix
// before
let tgt = "abc123"; // commit not present in shallow clone
reset_hard(worktree, tgt)?; // git reset --hard abc123 failed: unknown revision
// after
// ensure the object exists before resetting
if !rev_parse_ok(worktree, tgt) {
run_in(worktree, &["git", "fetch", "origin", tgt]);
}
reset_hard(worktree, tgt)?;
Defensive patterns
Strategy: validation
Validate before calling
use std::process::Command;
use std::path::Path;
fn ref_exists(worktree: &Path, tgt: &str) -> bool {
Command::new("git").current_dir(worktree)
.args(["rev-parse", "--verify", "--quiet", &format!("{tgt}^{{commit}}")])
.output().map(|o| o.status.success()).unwrap_or(false)
}
assert!(ref_exists(worktree_path, tgt), "target ref missing in worktree");
assert!(!worktree_path.join(".git").join("index.lock").exists(), "stale index.lock"); Try / catch
match reset_hard(worktree, tgt) {
Err(e) if e.to_string().contains("git reset --hard") => {
eprintln!("reset failed: {e}"); // stderr embedded
// fetch missing objects / clear index.lock, then retry once
}
r => r?,
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- git clean {} failed: {}
- git checkout {} failed: {}
- git worktree add failed: {}
- git worktree add failed: {}
- invalid .git file format: {}
AI-assisted analysis of xai-org/grok-build@bc7f02eddd (2026-08-31).
Data as JSON: /api/errors/8f2f27df8915fbb2.
Report an issue: GitHub.