github
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUse the CLI for everything. The user's OAuth access token is exported
as ; reads it automatically — will say
"not logged in" because gh keeps no config file in the sandbox, but every
authenticated subcommand works regardless.
gh$GH_TOKENghgh auth statusgh --helpgh <subcommand> --help所有操作都使用 CLI完成。用户的OAuth访问令牌会导出为;会自动读取该令牌——会显示“未登录”,因为gh在沙箱中不保存配置文件,但所有需要认证的子命令仍可正常工作。
gh$GH_TOKENghgh auth statusgh --helpgh <subcommand> --helpTwo ways to call gh — prefer subcommands
两种调用gh的方式——优先使用子命令
Style A: First-class subcommands — START HERE
方式A:一级子命令——从这里开始
gh issuegh prgh repogh searchgh releasegh workflowgh rungh statusgh projectgh labelgh secretgh variablegh gist--json <fields> [--jq <expr>]gh issuegh prgh repogh searchgh releasegh workflowgh rungh statusgh projectgh labelgh secretgh variablegh gist--json <fields> [--jq <expr>]Style B: Raw REST / GraphQL via gh api
gh api方式B:通过gh api
调用原生REST / GraphQL
gh apigh api <endpoint>gh api graphql -f query='…'- — override method (default
-X POST|PATCH|PUT|DELETE, becomesGETautomatically whenPOST/-fis set).-F - — string field;
-f key=value— JSON-typed field (-F key=value/true/123); both URL-encode for@file.jsonand JSON-encode for body methods.GET - — same as
-q '<jq>'. With a primitive top-level value (string / number) it prints the raw value (no quotes).--jq - — fetch a file's raw bytes instead of the JSON wrapper.
-H 'Accept: application/vnd.github.raw' - — auto-walk
--paginate.Link: rel="next"
使用调用REST接口,使用调用GraphQL接口。当没有对应的一级子命令时,这种方式非常有用。常用参数:
gh api <endpoint>gh api graphql -f query='…'- —— 覆盖请求方法(默认是
-X POST|PATCH|PUT|DELETE,当设置GET/-f时会自动变为-F)。POST - —— 字符串字段;
-f key=value—— JSON类型字段(-F key=value/true/123);对于@file.json请求,两者都会进行URL编码;对于请求体方法,都会进行JSON编码。GET - —— 与
-q '<jq>'功能相同。如果是顶层原始值(字符串/数字),会直接打印原始值(不带引号)。--jq - —— 获取文件的原始字节数据,而非JSON包装格式。
-H 'Accept: application/vnd.github.raw' - —— 自动遍历
--paginate链接进行分页。Link: rel="next"
Recipes
操作示例
Triage what's on my plate (issues + PRs + reviews + mentions)
查看我的待处理事项(issues + PRs + 评审 + 提及)
sh
gh statussh
gh statusList recent issues in a repo
列出仓库中最近的issues
sh
gh issue list --repo OWNER/REPO --limit 20
gh issue list --repo OWNER/REPO --state all --limit 20 \
--json number,title,state,author,updatedAt,labels --jq '.[]'sh
gh issue list --repo OWNER/REPO --limit 20
gh issue list --repo OWNER/REPO --state all --limit 20 \
--json number,title,state,author,updatedAt,labels --jq '.[]'View an issue with comments
查看包含评论的issue
sh
gh issue view 123 --repo OWNER/REPO --comments
gh issue view 123 --repo OWNER/REPO --json title,body,comments \
--jq '{title, body, comments: [.comments[] | {author: .author.login, body, createdAt}]}'sh
gh issue view 123 --repo OWNER/REPO --comments
gh issue view 123 --repo OWNER/REPO --json title,body,comments \
--jq '{title, body, comments: [.comments[] | {author: .author.login, body, createdAt}]}'Create / comment / close an issue
创建/评论/关闭issue
sh
gh issue create --repo OWNER/REPO --title "Bug: foo" --body "Repro steps…" --label bug
gh issue comment 123 --repo OWNER/REPO --body "LGTM"
gh issue close 123 --repo OWNER/REPO --comment "Fixed in #456"sh
gh issue create --repo OWNER/REPO --title "Bug: foo" --body "复现步骤…" --label bug
gh issue comment 123 --repo OWNER/REPO --body "LGTM"
gh issue close 123 --repo OWNER/REPO --comment "已在#456中修复"List PRs assigned to / authored by me
列出分配给我/由我创建的PRs
sh
gh search prs --assignee=@me --state=open --json number,title,repository,updatedAt
gh search prs --author=@me --state=opensh
gh search prs --assignee=@me --state=open --json number,title,repository,updatedAt
gh search prs --author=@me --state=openView a PR with diff and CI checks
查看包含差异和CI检查的PR
sh
gh pr view 456 --repo OWNER/REPO
gh pr diff 456 --repo OWNER/REPO
gh pr checks 456 --repo OWNER/REPOsh
gh pr view 456 --repo OWNER/REPO
gh pr diff 456 --repo OWNER/REPO
gh pr checks 456 --repo OWNER/REPOComment / review / merge a PR
评论/评审/合并PR
sh
gh pr comment 456 --repo OWNER/REPO --body "Please rebase on main."
gh pr review 456 --repo OWNER/REPO --approve --body "LGTM"
gh pr review 456 --repo OWNER/REPO --request-changes --body "See nits"
gh pr merge 456 --repo OWNER/REPO --squash --delete-branchsh
gh pr comment 456 --repo OWNER/REPO --body "请基于main分支变基。"
gh pr review 456 --repo OWNER/REPO --approve --body "LGTM"
gh pr review 456 --repo OWNER/REPO --request-changes --body "查看细节问题"
gh pr merge 456 --repo OWNER/REPO --squash --delete-branchSearch code across GitHub
在GitHub上搜索代码
sh
gh search code 'someFunction language:typescript' --limit 20 \
--json repository,path,url --jq '.[] | "\(.repository.nameWithOwner) \(.path)"'sh
gh search code 'someFunction language:typescript' --limit 20 \
--json repository,path,url --jq '.[] | "\(.repository.nameWithOwner) \(.path)"'Read a file from a repo (raw bytes, no base64 dance)
从仓库中读取文件(原始字节,无需base64转换)
sh
gh api "repos/OWNER/REPO/contents/path/to/file.ts" \
-H 'Accept: application/vnd.github.raw'sh
gh api "repos/OWNER/REPO/contents/path/to/file.ts" \
-H 'Accept: application/vnd.github.raw'List recent commits on the default branch
列出默认分支上最近的提交
sh
gh api "repos/OWNER/REPO/commits?per_page=20" \
--jq '.[] | "\(.sha[0:7]) \(.commit.author.date) \(.commit.message | split("\n")[0])"'sh
gh api "repos/OWNER/REPO/commits?per_page=20" \
--jq '.[] | "\(.sha[0:7]) \(.commit.author.date) \(.commit.message | split("\n")[0])"'Trigger / inspect Actions workflows
触发/查看Actions工作流
sh
gh workflow list --repo OWNER/REPO
gh workflow run ci.yaml --repo OWNER/REPO --ref main -f key=value
gh run list --repo OWNER/REPO --workflow ci.yaml --limit 5
gh run view <RUN_ID> --repo OWNER/REPO --log-failedsh
gh workflow list --repo OWNER/REPO
gh workflow run ci.yaml --repo OWNER/REPO --ref main -f key=value
gh run list --repo OWNER/REPO --workflow ci.yaml --limit 5
gh run view <RUN_ID> --repo OWNER/REPO --log-failedView a repo's metadata
查看仓库的元数据
sh
gh repo view OWNER/REPO
gh repo view OWNER/REPO --json description,url,stargazerCount,defaultBranchRefsh
gh repo view OWNER/REPO
gh repo view OWNER/REPO --json description,url,stargazerCount,defaultBranchRefGraphQL for things REST can't do (e.g. project board items)
使用GraphQL完成REST无法实现的操作(例如项目看板项)
sh
gh api graphql -f query='
query($owner: String!, $repo: String!, $num: Int!) {
repository(owner: $owner, name: $repo) {
issue(number: $num) {
title
timelineItems(first: 50) {
nodes { __typename ... on CrossReferencedEvent { source { ... on PullRequest { number title state } } } }
}
}
}
}' -f owner=OWNER -f repo=REPO -F num=123sh
gh api graphql -f query='
query($owner: String!, $repo: String!, $num: Int!) {
repository(owner: $owner, name: $repo) {
issue(number: $num) {
title
timelineItems(first: 50) {
nodes { __typename ... on CrossReferencedEvent { source { ... on PullRequest { number title state } } } }
}
}
}
}' -f owner=OWNER -f repo=REPO -F num=123Notes
注意事项
- For private repos the user MUST have granted scope when they authorized the connection at
repo. A 404 on a repo you know exists usually means missing scope, not a wrong URL.auth.acedata.cloud/user/connections - When rejects a field name, gh prints the full list of valid fields — re-read the error and pick from there.
--json - and
gh issue list --searchuse the GitHub search syntax (gh search issues,is:open,assignee:@me, etc.). Userepo:owner/name/gh search issuesfor cross-repo queries; usegh search prsfor one repo.gh issue list - only works on endpoints that emit a
gh api --paginateheader; for cursor-paginated endpoints you have to followLinkyourself.pagination.next
- 对于私有仓库,用户在授权连接时必须授予
auth.acedata.cloud/user/connections权限。如果你确定仓库存在但返回404,通常意味着缺少权限,而非URL错误。repo - 当拒绝某个字段名时,gh会打印所有有效字段列表——请重新阅读错误信息并从中选择。
--json - 和
gh issue list --search使用GitHub搜索语法(gh search issues、is:open、assignee:@me等)。跨仓库查询使用repo:owner/name/gh search issues;单仓库查询使用gh search prs。gh issue list - 仅适用于返回
gh api --paginate头的接口;对于基于游标分页的接口,你需要自行处理Link。pagination.next