{"record":{"id":"1c60143ae5cac2c5","repo":"github/github-mcp-server","slug":"failed-to-get-branch-reference-w","errorCode":null,"errorMessage":"failed to get branch reference: %w","messagePattern":"failed to get branch reference: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/github/repositories.go","lineNumber":1121,"sourceCode":"\t\t\t}\n\t\t\tmessage, err := RequiredParam[string](args, \"message\")\n\t\t\tif err != nil {\n\t\t\t\treturn utils.NewToolResultError(err.Error()), nil, nil\n\t\t\t}\n\t\t\tbranch, err := RequiredParam[string](args, \"branch\")\n\t\t\tif err != nil {\n\t\t\t\treturn utils.NewToolResultError(err.Error()), nil, nil\n\t\t\t}\n\n\t\t\tclient, err := deps.GetClient(ctx)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, nil, fmt.Errorf(\"failed to get GitHub client: %w\", err)\n\t\t\t}\n\n\t\t\t// Get the reference for the branch\n\t\t\tref, resp, err := client.Git.GetRef(ctx, owner, repo, \"refs/heads/\"+branch)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, nil, fmt.Errorf(\"failed to get branch reference: %w\", err)\n\t\t\t}\n\t\t\tdefer func() { _ = resp.Body.Close() }()\n\n\t\t\t// Get the commit object that the branch points to\n\t\t\tbaseCommit, resp, err := client.Git.GetCommit(ctx, owner, repo, *ref.Object.SHA)\n\t\t\tif err != nil {\n\t\t\t\treturn ghErrors.NewGitHubAPIErrorResponse(ctx,\n\t\t\t\t\t\"failed to get base commit\",\n\t\t\t\t\tresp,\n\t\t\t\t\terr,\n\t\t\t\t), nil, nil\n\t\t\t}\n\t\t\tdefer func() { _ = resp.Body.Close() }()\n\n\t\t\tif resp.StatusCode != http.StatusOK {\n\t\t\t\tbody, err := io.ReadAll(resp.Body)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn nil, nil, fmt.Errorf(\"failed to read response body: %w\", err)","sourceCodeStart":1103,"sourceCodeEnd":1139,"githubUrl":"https://github.com/github/github-mcp-server/blob/0ea1f775a7c73eff1bd2e25904d01136756bbfe2/pkg/github/repositories.go#L1103-L1139","documentation":"delete_file resolves the branch head via client.Git.GetRef(ctx, owner, repo, \"refs/heads/\"+branch) before constructing the deletion commit; this error wraps any failure of that lookup. Unlike neighboring calls it uses a plain fmt.Errorf rather than ghErrors.NewGitHubAPIErrorResponse, so rate-limit detail and response context are lost. The dominant cause is go-github's ErrorResponse for 404/422 'Reference does not exist' — i.e., the branch is not there.","triggerScenarios":"Branch name typo or main-vs-master mismatch (repo's default branch differs from the argument); token lacks read access to the repo; owner/repo misspelled; branch argument with leading/trailing whitespace making the ref path invalid; empty repository with no refs.","commonSituations":"Calling delete_file with \"branch\":\"main\" on older repos using \"master\", fine-grained PATs missing Contents read/write, freshly renamed branches, stale cached branch names in automation scripts.","solutions":["Confirm the branch exists: call list_branches on the same owner/repo and match the name exactly (case-sensitive)","Check token permissions — fine-grained PATs need Contents: read and write on the target repo","Trim/validate the branch argument before calling delete_file","If the ref genuinely exists, retry — transient 5xx from the Git refs endpoint also surfaces here"],"exampleFix":"// before\nref, resp, err := client.Git.GetRef(ctx, owner, repo, \"refs/heads/\"+branch)\nif err != nil {\n\treturn nil, nil, fmt.Errorf(\"failed to get branch reference: %w\", err)\n}\n// after — validate the name and keep the structured error path\nif strings.TrimSpace(branch) == \"\" || strings.ContainsAny(branch, \" ~^:?[\\\\*\") {\n\treturn utils.NewToolResultError(\"invalid branch name\"), nil, nil\n}\nref, resp, err := client.Git.GetRef(ctx, owner, repo, \"refs/heads/\"+branch)\nif err != nil {\n\treturn ghErrors.NewGitHubAPIErrorResponse(ctx, \"failed to get branch reference\", resp, err), nil, nil\n}","handlingStrategy":"validation","validationCode":"// Verify the branch exists before calling delete_file\n_, _, err := client.Git.GetRef(ctx, owner, repo, \"refs/heads/\"+branch)\nif err != nil {\n\tvar ghErr *github.ErrorResponse\n\tif errors.As(err, &ghErr) && (ghErr.Response.StatusCode == 404 || ghErr.Response.StatusCode == 422) {\n\t\treturn fmt.Errorf(\"branch %q does not exist in %s/%s\", branch, owner, repo)\n\t}\n}","typeGuard":"func branchExists(err error) bool {\n\tvar ghErr *github.ErrorResponse\n\treturn !(errors.As(err, &ghErr) && ghErr.Response != nil &&\n\t\t(ghErr.Response.StatusCode == 404 || ghErr.Response.StatusCode == 422))\n}","tryCatchPattern":"if err != nil {\n\tvar ghErr *github.ErrorResponse\n\tif errors.As(err, &ghErr) {\n\t\tswitch ghErr.Response.StatusCode {\n\t\tcase 404, 422:\n\t\t\t// branch missing: fix the name, do not retry\n\t\tcase 403:\n\t\t\t// token lacks access: fix scopes\n\t\tdefault:\n\t\t\t// transient 5xx: retry with backoff\n\t\t}\n\t}\n}","preventionTips":["Discover the exact branch name with list_branches instead of assuming main/master","Trim and format-check branch arguments before delete_file","Use fine-grained PATs with Contents: read and write on the target repo","Remember this path returns a plain wrapped error, not a structured one — parse the message chain for the GitHub ErrorResponse detail"],"tags":["github-mcp-server","go","git-refs","github-api"],"backgroundTag":null,"analyzedSha":"0ea1f775a7c73eff1bd2e25904d01136756bbfe2","analyzedAt":"2026-08-15T18:10:19.804Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}