nikivdev/code · error
{hash} is not a valid git commit
Error message
{hash} is not a valid git commit What it means
resolve_git_commit_sha() runs `git rev-parse --verify <hash>^{commit}` inside the repo. If the command fails, or the returned SHA is empty, it throws this error indicating the supplied string is not a resolvable git commit in this repository. The git stderr is attached as context.
Source
Thrown at src/commit.rs:7135
let matches: Vec<_> = entries
.into_iter()
.filter(|entry| commit_queue_entry_matches(entry, hash))
.collect();
match matches.len() {
0 => bail!("No queued commit matches {}", hash),
1 => Ok(matches.into_iter().next().unwrap()),
_ => bail!("Multiple queued commits match {}. Use a longer hash.", hash),
}
}
fn resolve_git_commit_sha(repo_root: &Path, hash: &str) -> Result<String> {
let rev = format!("{hash}^{{commit}}");
let sha = git_capture_in(repo_root, &["rev-parse", "--verify", &rev])
.with_context(|| format!("{hash} is not a valid git commit"))?;
let trimmed = sha.trim();
if trimmed.is_empty() {
bail!("{hash} is not a valid git commit");
}
Ok(trimmed.to_string())
}
fn queue_existing_commit_for_approval(
repo_root: &Path,
hash: &str,
mark_reviewed: bool,
) -> Result<CommitQueueEntry> {
let commit_sha = resolve_git_commit_sha(repo_root, hash)?;
let branch = git_capture_in(repo_root, &["rev-parse", "--abbrev-ref", "HEAD"])
.unwrap_or_else(|_| "unknown".to_string())
.trim()
.to_string();
let message = git_capture_in(repo_root, &["log", "-1", "--format=%s", &commit_sha])
.unwrap_or_default()
.trim()
.to_string();View on GitHub (pinned to a747e741ae)
Solutions
- Run `git rev-parse --verify <hash>^{commit}` yourself to see git's own error
- Run `git fetch --all` if the commit may exist on a remote
- Correct the hash typo or use the full 40-char SHA from `git log`
- Check you are in the right repository/clone containing the commit
Example fix
// before (shell)
f commit approve deadbeef # unknown locally
// after (shell)
git fetch --all && git rev-parse --verify deadbeef^{commit} && f commit approve deadbeef Defensive patterns
Strategy: validation
Validate before calling
use std::process::Command;
fn commit_exists(repo: &std::path::Path, hash: &str) -> bool {
Command::new("git")
.current_dir(repo)
.args(["rev-parse", "--verify", &format!("{hash}^{{commit}}")])
.output()
.map(|o| o.status.success())
.unwrap_or(false)
} Try / catch
if let Err(e) = tool.resolve_commit_sha(repo, hash) {
if e.to_string().contains("is not a valid git commit") {
let _ = Command::new("git").current_dir(repo).args(["fetch", "--all"]).status();
// then retry or surface guidance to the user
} else { return Err(e.into()); }
} Prevention
- `git fetch --all` before referencing remote commits
- Validate refs with `git rev-parse --verify` before tooling calls
- Use full 40-char SHAs in automation
- Watch for shallow clones (--depth) missing history
When it happens
Trigger: Passing a hash that does not exist locally (not fetched), a branch/tag name that isn't a commit, a mistyped hash, or an abbreviated hash too short for git to resolve unambiguously.
Common situations: Referencing a commit from a remote that hasn't been fetched, typos in the hash, using a tag that was deleted, or running in a shallow clone where the commit is missing.
Related errors
- jj git export retry loop should always return
- Lin.app is not running
- No branches available to search
- No branches matched query '{}'.
- No branches available for AI matching
AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01).
Data as JSON: /api/errors/448c3c8e7ecf49a5.
Report an issue: GitHub.