using-gh-cli
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUsing the GitHub CLI (gh
)
gh使用GitHub CLI(gh
)
ghWhen 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, orwgetforWebFetch
- 从GitHub仓库浏览或查看代码——克隆仓库后使用Read/Glob/Grep工具
- 查看或创建拉取请求(PR)、议题(issues)、版本发布(releases)或Gist
- 获取仓库元数据或任何GitHub API数据
- 与GitHub Actions交互(工作流运行、工作流管理)
- 任何原本可能使用、
curl或wget处理的GitHub相关任务WebFetch
When NOT to Use
不适用场景
- Non-GitHub URLs (use or
WebFetchfor those)curl - Public web content that happens to be hosted on GitHub Pages () — those are regular websites
*.github.io - Local git operations (,
git commit) — usegit pushdirectlygit
- 非GitHub的URL(此类情况使用或
WebFetch)curl - 托管在GitHub Pages()上的公开网页内容——这些属于常规网站
*.github.io - 本地Git操作(、
git commit)——直接使用git push命令git
Key Principle
核心原则
Always use instead of , , or for GitHub URLs. The CLI uses the user's authenticated token automatically, so it:
ghcurlwgetWebFetchgh- 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
针对GitHub相关的URL,始终使用而非、或。 CLI会自动使用用户的已认证令牌,因此它:
ghcurlwgetWebFetchgh- 可访问私有仓库
- 避免GitHub API速率限制(未认证:60次请求/小时;已认证:5000次请求/小时)
- 正确处理分页
- 提供结构化输出和过滤功能
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.
bash
undefined要读取或浏览GitHub仓库中的文件,请将其克隆到本地后使用常规文件工具(Read、Glob、Grep)。这比通过API逐个获取文件要快得多,也更符合使用习惯。
bash
undefinedClone to a session-scoped temp directory (cleaned up automatically on session end)
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 cloningclonedir="$TMPDIR/gh-clones-${CLAUDE_SESSION_ID}"
mkdir -p "$clonedir"
gh repo clone owner/repo "$clonedir/repo" -- --depth 1
克隆完成后,使用**Explore Agent**(通过Task工具,设置`subagent_type=Explore`)来研究克隆后的仓库。Explore Agent可以高效地在克隆仓库中使用Read、Glob和Grep——比逐个调用这些工具要好得多:
Task(subagent_type="Explore", prompt="In $clonedir/repo/, find how authentication is implemented")
对于已熟悉的克隆仓库,若只需针对性查找内容,可直接使用Read/Glob/Grep工具。
- `gh repo clone`会使用用户的已认证令牌——可访问私有仓库
- `--depth 1`参数确保克隆速度快(仅获取最新提交)
- 无需手动清理——SessionEnd钩子会自动删除克隆目录
- 仅当需要快速单个文件查找且无需克隆时,才使用`gh api`Quick Start
快速入门
bash
undefinedbash
undefinedView a repo
View a repo
gh repo view owner/repo
gh repo view owner/repo
List and view PRs
List and view PRs
gh pr list --repo owner/repo
gh pr view 123 --repo owner/repo
gh pr list --repo owner/repo
gh pr view 123 --repo owner/repo
List and view issues
List and view issues
gh issue list --repo owner/repo
gh issue view 456 --repo owner/repo
gh issue list --repo owner/repo
gh issue view 456 --repo owner/repo
Call any REST API endpoint
Call any REST API endpoint
gh api repos/owner/repo/contents/README.md
gh api repos/owner/repo/contents/README.md
Call with pagination and jq filtering
Call with pagination and jq filtering
gh api repos/owner/repo/pulls --paginate --jq '.[].title'
undefinedgh api repos/owner/repo/pulls --paginate --jq '.[].title'
undefinedCommon Patterns
常见用法对比
| Instead of | Use |
|---|---|
| |
| Clone with |
| Clone with |
| |
| |
| |
| |
| 替代方案 | 推荐用法 |
|---|---|
| |
| 使用 |
| 使用 |
| |
| |
| |
| |
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
- Pull Requests — 列出、查看、创建、合并、评审PR
- Issues — 列出、查看、创建、关闭、评论议题
- Repos and Files — 查看仓库、浏览文件、克隆
- API — 原生REST/GraphQL访问、分页、jq过滤
- Releases — 列出、创建、下载版本发布
- Actions — 查看运行记录、触发工作流、检查日志