{"record":{"id":"e2fc5032c51f7167","repo":"GoogleContainerTools/skaffold","slug":"failed-to-checkout-commit-w","errorCode":null,"errorMessage":"failed to checkout commit: %w","messagePattern":"failed to checkout commit: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/skaffold/git/gitutil.go","lineNumber":139,"sourceCode":"\t}\n\trepoCacheDir := filepath.Join(skaffoldCacheDir, hash)\n\tif _, err := os.Stat(repoCacheDir); os.IsNotExist(err) {\n\t\tif opts.SyncRemoteCache.CloneDisabled() {\n\t\t\treturn \"\", SyncDisabledErr(g, repoCacheDir)\n\t\t}\n\t\tif _, err := r.Run(ctx, \"clone\", g.RepoCloneURI, fmt.Sprintf(\"./%s\", hash), \"--branch\", ref, \"--depth\", \"1\"); err != nil {\n\t\t\tif strings.Contains(err.Error(), \"Could not find remote branch\") {\n\t\t\t\tif _, err := r.Run(ctx, \"clone\", g.RepoCloneURI, fmt.Sprintf(\"./%s\", hash), \"--depth\", \"1\"); err != nil {\n\t\t\t\t\treturn \"\", fmt.Errorf(\"failed to clone repo: %w\", err)\n\t\t\t\t}\n\n\t\t\t\tr.Dir = repoCacheDir\n\t\t\t\tif _, err := r.Run(ctx, \"checkout\", ref); err != nil {\n\t\t\t\t\tif rmErr := os.RemoveAll(repoCacheDir); rmErr != nil {\n\t\t\t\t\t\terr = fmt.Errorf(\"failed to remove repo cache dir: %w\", rmErr)\n\t\t\t\t\t}\n\n\t\t\t\t\treturn \"\", fmt.Errorf(\"failed to checkout commit: %w\", err)\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\treturn \"\", fmt.Errorf(\"failed to clone repo: %w\", err)\n\t\t\t}\n\t\t}\n\t} else {\n\t\tr.Dir = repoCacheDir\n\t\t// check remote is defined\n\t\tif remotes, err := r.Run(ctx, \"remote\", \"-v\"); err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to clone repo %s: trouble checking repository remote; run 'git clone <REPO>; stat <DIR/SUBDIR>' to verify credentials: %w\", g.Repo, err)\n\t\t} else if len(remotes) == 0 {\n\t\t\treturn \"\", fmt.Errorf(\"failed to clone repo %s: remote not set for existing clone\", g.Repo)\n\t\t}\n\n\t\t// if sync property is false, then skip fetching latest from remote and resetting the branch.\n\t\tif g.Sync != nil && !*g.Sync {\n\t\t\treturn repoCacheDir, nil\n\t\t}","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/GoogleContainerTools/skaffold/blob/a1189de023efc32d4b8e11f395acc678aa555011/pkg/skaffold/git/gitutil.go#L121-L157","documentation":"Thrown by syncRepo when the fallback clone succeeded but `git checkout <ref>` fails inside the fresh cache clone. This happens when the requested ref is not present in the default-branch-only shallow clone (depth 1). If the partial cache dir could not be removed, its removal error is additionally wrapped into this error.","triggerScenarios":"git clone succeeded without --branch (because --branch <ref> said the ref was missing), then git checkout <ref> also fails: ref genuinely doesn't exist, or exists only in history beyond the depth-1 shallow clone / on other branches.","commonSituations":"Requesting an old commit SHA with a shallow clone; ref exists on a non-default branch not fetched by depth-1 clone; ref name typo (case-sensitive); the remote branch was force-pushed/renamed between attempts.","solutions":["Verify the ref exists on the remote: git ls-remote <URI> <ref>, and correct the ref in config.","Use a branch or tag name instead of a raw commit SHA — shallow clones only fetch the branch tip.","Manually remove the partial cache directory (<remote-cache-dir>/<hash>) so the next run re-clones cleanly.","Upgrade skaffold; newer versions handle shallow-clone checkout of arbitrary SHAs better."],"exampleFix":"// before\nrepo: https://github.com/org/repo#a1b2c3d   # SHA not in shallow depth-1 clone\n// after\nrepo: https://github.com/org/repo#main","handlingStrategy":"validation","validationCode":"const { execSync } = require('child_process');\nfunction refIsBranchOrTag(uri, ref) {\n  const out = execSync(`git ls-remote ${uri} refs/heads/${ref} refs/tags/${ref}`, { encoding: 'utf8' });\n  return out.trim().length > 0;\n}","typeGuard":"function isResolvableRef(ref) {\n  // branch/tag names are shallow-clone safe; raw SHAs are not\n  return /^[A-Za-z0-9._/-]+$/.test(ref) && !/^[0-9a-f]{40}$/i.test(ref);\n}","tryCatchPattern":"try {\n  await syncRepo(g, ctx, opts);\n} catch (err) {\n  if (/failed to checkout commit/.test(err.message)) {\n    fs.rmSync(path.join(cacheDir, hashFor(g)), { recursive: true, force: true });\n    throw new Error(`Ref ${g.ref} not available in shallow clone; use a branch/tag or full clone.`);\n  }\n  throw err;\n}","preventionTips":["Configure branch/tag refs, not commit SHAs, when relying on skaffold's depth-1 clones.","Confirm refs with git ls-remote before adding them to config.","After any failure, rm -rf the per-repo cache dir so the next attempt is clean.","If SHAs are required, pre-clone the repo yourself with a full fetch."],"tags":["git","checkout","shallow-clone"],"backgroundTag":"checkout-ref-not-found","analyzedSha":"a1189de023efc32d4b8e11f395acc678aa555011","analyzedAt":"2026-09-05T12:09:27.064Z","contentChangedAt":"2026-09-05T12:09:27.064Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}