nikivdev/code · error

failed to resolve HEAD commit

Error message

failed to resolve HEAD commit

What it means

land_head_to_branch resolves the current HEAD commit via `git rev-parse HEAD` before cherry-picking it onto the target branch. If the git command produces no output, the function throws this error instead of proceeding with an empty SHA.

Source

Thrown at src/git_guard.rs:144

}

fn land_head_to_branch(repo_root: &Path, requested_target: &str) -> Result<()> {
    ensure_clean_working_tree_for_land(repo_root)?;

    let current = git_capture_in(repo_root, &["rev-parse", "--abbrev-ref", "HEAD"])
        .unwrap_or_else(|| "HEAD".to_string());
    if current.trim() == "HEAD" {
        bail!("HEAD is detached. Run `f git-repair` first.");
    }
    let current = current.trim().to_string();
    let target = resolve_land_target_branch(repo_root, requested_target)?;
    if current == target {
        println!("Already on {}", target);
        return Ok(());
    }

    let head_sha = git_capture_in(repo_root, &["rev-parse", "HEAD"])
        .ok_or_else(|| anyhow::anyhow!("failed to resolve HEAD commit"))?;

    git_run_in(repo_root, &["checkout", &target])?;
    match git_run_in(repo_root, &["cherry-pick", head_sha.trim()]) {
        Ok(_) => {
            println!(
                "✓ Landed commit {} from {} onto {}",
                short_sha(head_sha.trim()),
                current,
                target
            );
            Ok(())
        }
        Err(err) => {
            let conflicts = git_unmerged_files(repo_root);
            let _ = git_run_in(repo_root, &["cherry-pick", "--abort"]);
            let _ = git_run_in(repo_root, &["checkout", &current]);

            eprintln!(

View on GitHub (pinned to a747e741ae)

Solutions

  1. Make an initial commit first (git commit --allow-empty) so HEAD resolves
  2. Verify repo_root points at a real git work tree (git -C <root> status)
  3. Check .git/HEAD and branch refs are not corrupted; run git fsck
  4. Ensure you're not in a bare repo when a commit is expected

Example fix

// before
let head_sha = git_capture_in(repo_root, &["rev-parse", "HEAD"])
    .ok_or_else(|| anyhow::anyhow!("failed to resolve HEAD commit"))?;
// after
if git_capture_in(repo_root, &["rev-parse", "--verify", "HEAD"]).is_none() {
    anyhow::bail!("no commits on HEAD; create a commit before landing");
}
let head_sha = git_capture_in(repo_root, &["rev-parse", "HEAD"])
    .ok_or_else(|| anyhow::anyhow!("failed to resolve HEAD commit"))?;
Defensive patterns

Strategy: validation

Validate before calling

fn has_commits(root: &Path) -> bool {
    std::process::Command::new("git").args(["-C"]).arg(root)
        .args(["rev-parse", "--verify", "HEAD"])
        .output().map(|o| o.status.success()).unwrap_or(false)
}

Try / catch

match land_head_to_branch(root, target) {
    Err(e) if e.to_string().contains("resolve HEAD") => {
        eprintln!("repo has no commits; commit first");
    }
    other => other?,
}

Prevention

When it happens

Trigger: Running `run_git_repair` land flow in a repository with no commits (unborn HEAD, freshly `git init`), a corrupted HEAD ref, or running outside a valid work tree so git_capture_in returns None.

Common situations: Repo with zero commits, detached/broken .git/HEAD pointing to a missing ref, wrong working directory passed as repo_root.

Related errors


AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01). Data as JSON: /api/errors/ced0fe7b200b3cc6. Report an issue: GitHub.