{"record":{"id":"0d0fc32edf3911cd","repo":"GoogleContainerTools/skaffold","slug":"failed-to-get-default-branch-for-repo-s","errorCode":null,"errorMessage":"failed to get default branch for repo %s","messagePattern":"failed to get default branch for repo (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/skaffold/git/gitutil.go","lineNumber":64,"sourceCode":"// defaultRef returns the default ref as \"master\" if master branch exists in\n// remote repository, falls back to \"main\" if master branch doesn't exist\nfunc defaultRef(ctx context.Context, repoCloneURI, repo string) (string, error) {\n\tmasterRef := \"master\"\n\tmainRef := \"main\"\n\tmasterExists, err := branchExists(ctx, repoCloneURI, repo, masterRef)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tmainExists, err := branchExists(ctx, repoCloneURI, repo, mainRef)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tif masterExists {\n\t\treturn masterRef, nil\n\t} else if mainExists {\n\t\treturn mainRef, nil\n\t}\n\treturn \"\", fmt.Errorf(\"failed to get default branch for repo %s\", repo)\n}\n\n// BranchExists checks if branch is present in the input repo\nfunc branchExists(ctx context.Context, repoCloneURI, repo, branch string) (bool, error) {\n\tgitProgram, err := findGit()\n\tif err != nil {\n\t\treturn false, err\n\t}\n\tout, err := util.RunCmdOut(ctx, exec.Command(gitProgram, \"ls-remote\", \"--heads\", repoCloneURI, branch))\n\tif err != nil {\n\t\t// stdErr contains the error message for os related errors, git permission errors\n\t\t// and if repo doesn't exist\n\t\treturn false, fmt.Errorf(\"failed to lookup %s branch for repo %s: %w\", branch, repo, err)\n\t}\n\t// stdOut contains the branch information if the branch is present in remote repo\n\t// stdOut is empty if the repo doesn't have the input branch\n\tif strings.TrimSpace(string(out)) != \"\" {\n\t\treturn true, nil","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/GoogleContainerTools/skaffold/blob/a1189de023efc32d4b8e11f395acc678aa555011/pkg/skaffold/git/gitutil.go#L46-L82","documentation":"When no ref is specified for a remote git dependency, defaultRef probes the remote with git ls-remote for a 'master' branch and then a 'main' branch. If neither ls-remote succeeds in finding either branch, it returns this error. It means skaffold could not determine which default branch to clone.","triggerScenarios":"Calling syncRepo (via SyncRepo) with git.Config.Ref == \"\" for a repository whose remote has neither a master nor a main branch, so both branchExists probes return false with no error.","commonSituations":"Repos renamed default branch to something else (trunk, develop, mainline); repository is empty (no branches yet); bare/custom git server with unusual default branch.","solutions":["Pin the ref explicitly in the skaffold git dependency config (git.Config.Ref) instead of relying on default detection.","If the default branch has another name (e.g. trunk), set that name as the Ref.","If the repo is empty, push at least one branch before using it as a dependency.","Verify with git ls-remote --heads <repo> which branches actually exist remotely."],"exampleFix":"// before\ngit:\n  repo: https://github.com/org/repo\n  # no ref -> default-branch probe fails\n// after\ngit:\n  repo: https://github.com/org/repo\n  ref: trunk   # pin the actual default branch","handlingStrategy":"validation","validationCode":"git ls-remote --heads https://github.com/org/repo | grep -E 'refs/heads/(master|main)$' \\\n  || echo \"Neither master nor main exists; pin the ref explicitly in the skaffold git dependency\"","typeGuard":"func hasPinnedRef(g git.Config) bool {\n    return strings.TrimSpace(g.Ref) != \"\"\n}","tryCatchPattern":"path, err := git.SyncRepo(ctx, cfg, opts)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to get default branch for repo\") {\n        log.Warnf(\"no master/main on %s; pinning ref explicitly\", cfg.Repo)\n        cfg.Ref = detectBranchViaLsRemote(ctx, cfg.RepoCloneURI) // e.g. first head\n        return git.SyncRepo(ctx, cfg, opts)\n    }\n    return err\n}","preventionTips":["Always pin ref in git dependency configs rather than relying on default-branch detection.","If you rename default branches, update all skaffold configs referencing the old name.","For empty repos, push an initial branch before using them as dependencies.","CI: pre-run git ls-remote to assert the expected branch exists before invoking skaffold."],"tags":["git","repository","configuration"],"backgroundTag":"default-branch-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"}