| name | github-cli |
| description | Use when interacting with GitHub repos, PRs, issues, releases, or API data. Covers gh CLI usage patterns, authentication, and common queries. Triggers: "gh", "github", "pull request", "PR", "issue", "gh api", "gh pr", "gh issue", "github release" |
GitHub CLI (gh)
Patterns for using the GitHub CLI to interact with repos, PRs, issues, and the GitHub API — without leaving the terminal.
When to Use
- Fetching GitHub data (PRs, issues, commits, releases, repo info)
- Creating or reviewing pull requests from the terminal
- Querying the GitHub API for structured data
- Replacing WebFetch for GitHub URLs (which fails on JS-rendered pages)
Quick Reference
| Fact | Value |
|---|
| Tool | gh (GitHub CLI) |
| Auth | gh auth login or GITHUB_TOKEN env var |
| Docs | gh help <command> or man gh-<command> |
| API base | gh api wraps https://api.github.com/ with auth |
Why gh Over WebFetch
GitHub pages are JS-rendered SPAs — WebFetch only gets empty HTML/CSS scaffolding. The gh CLI uses authenticated API calls and returns structured JSON, making it the reliable choice for all GitHub interactions.
Common Patterns
Pull Requests
gh pr list
gh pr view 123
gh pr view https://github.com/owner/repo/pull/123
gh pr create --title "Title" --body "Description"
gh pr checks 123
gh pr diff 123
gh api repos/{owner}/{repo}/pulls/123/comments
Issues
gh issue list
gh issue view 42
gh issue create --title "Bug: ..." --body "Steps to reproduce..."
gh issue list --search "label:bug sort:updated-desc"
Repository Info
gh repo view owner/repo
gh release list
gh release view --repo owner/repo
gh repo clone owner/repo
Raw API Queries
gh api is the most powerful pattern — it wraps any GitHub REST or GraphQL endpoint with automatic authentication and pagination.
gh api repos/owner/repo
gh api repos/owner/repo/commits?sha=main&per_page=5
gh api repos/owner/repo/actions/runs/12345
gh api --paginate repos/owner/repo/issues
gh api graphql -f query='{ repository(owner:"owner", name:"repo") { stargazerCount } }'
gh api repos/owner/repo/pulls --jq '.[].title'
Authentication
gh auth login
gh auth status
GIT_SSH_COMMAND="ssh -i ~/.ssh/my_key -o IdentitiesOnly=yes" git push
Anti-Patterns
| Don't | Do Instead |
|---|
WebFetch on GitHub URLs | gh api or gh pr view |
curl with manual auth headers | gh api (handles auth automatically) |
| Parse GitHub HTML | Use gh api for structured JSON |
| Hardcode GitHub tokens in scripts | Use gh auth or GITHUB_TOKEN env var |
Tips
--jq flag: Filter JSON output inline without piping to jq. E.g., gh api repos/o/r/pulls --jq '.[].title'.
--paginate: Automatically follows pagination links for large result sets.
-X POST: Use with gh api for write operations. E.g., gh api repos/o/r/issues -f title="Bug" -f body="...".
--json flag: On gh pr list, gh issue list, etc., select specific fields: gh pr list --json number,title,author.
See Also
fail-fast-ml-engineering — Preflight checks that may query GitHub for repo state
wandb-experiment-tracking — W&B run metadata includes git commit info