Using the GitHub CLI (gh)
When to Use
- Browsing or reading code from a GitHub repository — clone it and use Read/Glob/Grep
- Viewing or creating pull requests, issues, releases, or gists
- Fetching repo metadata or any GitHub API data
- Interacting with GitHub Actions (runs, workflows)
- Any task involving GitHub that you might otherwise use
curl, wget, or WebFetch for
When NOT to Use
- Non-GitHub URLs (use
WebFetch or curl for those)
- Public web content that happens to be hosted on GitHub Pages (
*.github.io) — those are regular websites
- Local git operations (
git commit, git push) — use git directly
Key Principle
Always use gh instead of curl, wget, or WebFetch for GitHub URLs. The gh CLI uses the user's authenticated token automatically, so it:
- Works with private repositories
- Avoids GitHub API rate limits (unauthenticated: 60 req/hr; authenticated: 5,000 req/hr)
- Handles pagination correctly
- Provides structured output and filtering
Browsing Repository Code
To read or browse files from a GitHub repo, clone it locally and use normal file tools (Read, Glob, Grep). This is much faster and more natural than fetching files one-by-one via the API.
# Clone to a session-scoped temp directory (cleaned up automatically on session end)
clonedir="$TMPDIR/gh-clones-${CLAUDE_SESSION_ID}"
mkdir -p "$clonedir"
gh repo clone owner/repo "$clonedir/repo" -- --depth 1
After cloning, use the Explore agent (via the Task tool with subagent_type=Explore) to investigate the cloned repo. The Explore agent can use Read, Glob, and Grep across the clone efficiently — much better than calling them one at a time:
Task(subagent_type="Explore", prompt="In $clonedir/repo/, find how authentication is implemented")
For targeted lookups on a clone you already understand, use Read/Glob/Grep directly.
gh repo clone uses the user's authenticated token — works with private repos
--depth 1 keeps the clone fast (only latest commit)
- No cleanup needed — a SessionEnd hook removes the clone directory automatically
- Use
gh api only when you need a quick single-file lookup without cloning
Quick Start
# View a repo
gh repo view owner/repo
# List and view PRs
gh pr list --repo owner/repo
gh pr view 123 --repo owner/repo
# List and view issues
gh issue list --repo owner/repo
gh issue view 456 --repo owner/repo
# Call any REST API endpoint
gh api repos/owner/repo/contents/README.md
# Call with pagination and jq filtering
gh api repos/owner/repo/pulls --paginate --jq '.[].title'
Common Patterns
| Instead of |
Use |
WebFetch on github.com/owner/repo |
gh repo view owner/repo |
WebFetch on a blob/tree URL |
Clone with gh repo clone and use Read/Glob/Grep |
WebFetch on raw.githubusercontent.com/... |
Clone with gh repo clone and use Read |
WebFetch on api.github.com/... |
gh api <endpoint> |
curl https://api.github.com/... |
gh api <endpoint> |
curl with -H "Authorization: token ..." |
gh api <endpoint> (auth is automatic) |
wget to download a release asset |
gh release download --repo owner/repo |
References
- Pull Requests — list, view, create, merge, review PRs
- Issues — list, view, create, close, comment on issues
- Repos and Files — view repos, browse files, clone
- API — raw REST/GraphQL access, pagination, jq filtering
- Releases — list, create, download releases
- Actions — view runs, trigger workflows, check logs
1---2name: using-gh-cli-23description: Guides usage of the GitHub CLI (gh) for interacting with GitHub repositories, PRs, issues, and API. Use when working with GitHub resources instead of WebFetch or curl.4---5
6# Using the GitHub CLI (`gh`)
7
8## When to Use
9
10- **Browsing or reading code** from a GitHub repository — clone it and use Read/Glob/Grep
11- Viewing or creating pull requests, issues, releases, or gists
12- Fetching repo metadata or any GitHub API data
13- Interacting with GitHub Actions (runs, workflows)
14- Any task involving GitHub that you might otherwise use `curl`, `wget`, or `WebFetch` for
15
16## When NOT to Use
17
18- Non-GitHub URLs (use `WebFetch` or `curl` for those)
19- Public web content that happens to be hosted on GitHub Pages (`*.github.io`) — those are regular websites
20- Local git operations (`git commit`, `git push`) — use `git` directly
21
22## Key Principle
23
24**Always use `gh` instead of `curl`, `wget`, or `WebFetch` for GitHub URLs.** The `gh` CLI uses the user's authenticated token automatically, so it:
25
26- Works with private repositories
27- Avoids GitHub API rate limits (unauthenticated: 60 req/hr; authenticated: 5,000 req/hr)
28- Handles pagination correctly
29- Provides structured output and filtering
30
31## Browsing Repository Code
32
33**To read or browse files from a GitHub repo, clone it locally and use normal file tools** (Read, Glob, Grep). This is much faster and more natural than fetching files one-by-one via the API.
34
35```bash
36# Clone to a session-scoped temp directory (cleaned up automatically on session end)
37clonedir="$TMPDIR/gh-clones-${CLAUDE_SESSION_ID}"
38mkdir -p "$clonedir"
39gh repo clone owner/repo "$clonedir/repo" -- --depth 1
40```
41
42After cloning, use the **Explore agent** (via the Task tool with `subagent_type=Explore`) to investigate the cloned repo. The Explore agent can use Read, Glob, and Grep across the clone efficiently — much better than calling them one at a time:
43
44```
45Task(subagent_type="Explore", prompt="In $clonedir/repo/, find how authentication is implemented")
46```
47
48For targeted lookups on a clone you already understand, use Read/Glob/Grep directly.
49
50- `gh repo clone` uses the user's authenticated token — works with private repos
51- `--depth 1` keeps the clone fast (only latest commit)
52- No cleanup needed — a SessionEnd hook removes the clone directory automatically
53- Use `gh api` only when you need a quick single-file lookup without cloning
54
55## Quick Start
56
57```bash
58# View a repo
59gh repo view owner/repo
60
61# List and view PRs
62gh pr list --repo owner/repo
63gh pr view 123 --repo owner/repo
64
65# List and view issues
66gh issue list --repo owner/repo
67gh issue view 456 --repo owner/repo
68
69# Call any REST API endpoint
70gh api repos/owner/repo/contents/README.md
71
72# Call with pagination and jq filtering
73gh api repos/owner/repo/pulls --paginate --jq '.[].title'
74```
75
76## Common Patterns
77
78| Instead of | Use |
79|------------|-----|
80| `WebFetch` on `github.com/owner/repo` | `gh repo view owner/repo` |
81| `WebFetch` on a blob/tree URL | Clone with `gh repo clone` and use Read/Glob/Grep |
82| `WebFetch` on `raw.githubusercontent.com/...` | Clone with `gh repo clone` and use Read |
83| `WebFetch` on `api.github.com/...` | `gh api <endpoint>` |
84| `curl https://api.github.com/...` | `gh api <endpoint>` |
85| `curl` with `-H "Authorization: token ..."` | `gh api <endpoint>` (auth is automatic) |
86| `wget` to download a release asset | `gh release download --repo owner/repo` |
87
88## References
89
90- [Pull Requests](references/pull-requests.md) — list, view, create, merge, review PRs
91- [Issues](references/issues.md) — list, view, create, close, comment on issues
92- [Repos and Files](references/repos-and-files.md) — view repos, browse files, clone
93- [API](references/api.md) — raw REST/GraphQL access, pagination, jq filtering
94- [Releases](references/releases.md) — list, create, download releases
95- [Actions](references/actions.md) — view runs, trigger workflows, check logs