gh
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseReference
参考指南
Interactivity policy
交互性策略
ghmust provide --title and --body when not running interactivelyGH_PAGER--no-pagerghmust provide --title and --body when not running interactivelyGH_PAGER--no-pagerParsing JSON
解析JSON
Human output from is column-formatted. If you want structured data:
gh- Add for structured output.
--json field1,field2,... - Run a command with and no field list to print the full set of available fields, then pick what you need.
--json - Use for filtering without piping through a separate
--jq '<expr>'.jq - Use (alongside
--template '<go-template>') when you want shaped text output. Note that--json/--templatecollides with a body-template flag on a few commands (e.g.-T,gh pr create -T); always checkgh issue create -Tbefore assuming which one you're hitting.--help
gh- 添加以获取结构化输出。
--json field1,field2,... - 运行带但不指定字段列表的命令,打印所有可用字段后再选择所需内容。
--json - 使用进行过滤,无需通过单独的
--jq '<expr>'工具管道处理。jq - 当你需要自定义文本输出时,结合使用
--json(Go模板)。注意--template '<go-template>'/--template与部分命令的正文模板标志冲突(例如-T、gh pr create -T);使用前务必查看gh issue create -T确认你使用的是哪个标志。--help
Pagination and silent truncation
分页与静默截断
List commands cap results.
- ,
gh issue list,gh pr list: passgh search ...(-L N). The default is usually 30.--limit N - /
gh issue listdo not expose aggregate totals likegh pr listviatotalCount. If you need a true total, use--jsonto querygh api graphql; otherwise, treattotalCountas the cap for the current call.-L - For raw API calls use . Combine with
gh api --paginate <path>and (optionally)--jqto assemble one array.--slurp
列表类命令会限制结果数量。
- 、
gh issue list、gh pr list:传递gh search ...(-L N)参数。默认值通常为30。--limit N - /
gh issue list无法通过gh pr list暴露--json这类聚合统计值。若你需要真实总数,使用totalCount查询gh api graphql;否则,将totalCount视为当前调用的结果上限。-L - 对于原始API调用,使用。结合
gh api --paginate <path>和(可选)--jq来合并为一个数组。--slurp
Repo targeting
仓库定位
ghPass () to override the resolved CWD repo.
--repo OWNER/REPO-Rgh传递()参数可覆盖当前工作目录解析出的仓库。
--repo OWNER/REPO-RSearch vs list
搜索与列表的区别
- uses GitHub's search index and accepts the full search syntax (
gh search issues|prs|code|repos|commits|users,is:open,author:,label:,repo:owner/name, ...). Pass each qualifier as its own bare token, not as one quoted string:in:titleworks, butgh search issues repo:cli/cli is:open author:monalisais treated as a single keyword (parsed asgh search issues "repo:cli/cli is:open") and fails withrepo:"cli/cli is:open". Quote only multi-word free text (Invalid search query). Most qualifiers also have a dedicated flag (gh search issues "broken feature",--repo,--author, ...). Prefer search for anything cross-repo or filtered by author/label.--label - and
gh issue list --search "..."take the query as one quoted string (it is a flag value) and are scoped to one repo.gh pr list --search "..." - Bots author as GitHub Apps, so matches nothing. Use
--author dependabot(on--app dependabot/prandissue list; expands tosearch prs|issues) orauthor:app/<slug>.--author "dependabot[bot]"
- 使用GitHub的搜索索引,支持完整的搜索语法(
gh search issues|prs|code|repos|commits|users、is:open、author:、label:、repo:owner/name等)。每个限定符需作为独立的裸令牌传递,而非单个引号字符串:in:title是有效的,但gh search issues repo:cli/cli is:open author:monalisa会被视为单个关键词(解析为gh search issues "repo:cli/cli is:open"),并抛出repo:"cli/cli is:open"错误。仅对多词自由文本使用引号包裹(例如Invalid search query)。大多数限定符也有专用标志(gh search issues "broken feature"、--repo、--author等)。跨仓库或按作者/标签过滤的场景优先使用搜索功能。--label - 和
gh issue list --search "..."将查询内容作为单个引号字符串传递(作为标志值),且作用范围限定为单个仓库。gh pr list --search "..." - 机器人以GitHub应用身份创建内容,因此无法匹配任何结果。使用
--author dependabot(适用于--app dependabot/pr和issue list;会扩展为search prs|issues)或author:app/<slug>。--author "dependabot[bot]"
Issue types, sub-issues, and relationships
议题类型、子议题与关联关系
Newer subcommands model issue types, sub-issue hierarchy, and
blocked-by/blocking relationships.
gh issue- :
gh issue create,--type <name>(creates the new issue as a sub-issue),--parent <number|url>,--blocked-by <number|url,...>.--blocking <number|url,...> - (edits one or more issues in the same repo, e.g.
gh issue edit):gh issue edit 23 34/--type <name>,--remove-type/--parent <n|url>,--remove-parent/--add-sub-issue <n,n>,--remove-sub-issue <n,n>/--add-blocked-by <n,n>,--remove-blocked-by <n,n>/--add-blocking <n,n>. Relationship and parent refs are issue numbers or URLs; a URL may point to another repo on the same host, but a different host is rejected.--remove-blocking <n,n>cannot be used when editing more than one issue.--add-sub-issue - filters by issue type.
gh issue list --type <name> - and
gh issue viewaccept these asgh issue listfields (prefer them over scraping the default text output):--json,issueType,parent,subIssues,subIssuesSummary,blockedBy.blocking,subIssues, andblockedByare objects shapedblocking(not flat arrays), and{"nodes": [...], "totalCount": N}is capped (nodesat 100,subIssues/blockedByat 50), so compare the node count againstblockingto detect truncation.totalCount - GHES: issue types and sub-issues need 3.17+; blocked-by/blocking relationships need 3.19+.
较新的子命令支持议题类型、子议题层级以及被阻塞/阻塞关联关系的管理。
gh issue- :
gh issue create、--type <name>(将新议题创建为子议题)、--parent <number|url>、--blocked-by <number|url,...>。--blocking <number|url,...> - (可编辑同一仓库中的一个或多个议题,例如
gh issue edit):gh issue edit 23 34/--type <name>、--remove-type/--parent <n|url>、--remove-parent/--add-sub-issue <n,n>、--remove-sub-issue <n,n>/--add-blocked-by <n,n>、--remove-blocked-by <n,n>/--add-blocking <n,n>。关联关系和父议题引用可以是议题编号或URL;URL可指向同一主机上的其他仓库,但不同主机的URL会被拒绝。编辑多个议题时无法使用--remove-blocking <n,n>。--add-sub-issue - 按议题类型过滤结果。
gh issue list --type <name> - 和
gh issue view支持将以下内容作为gh issue list字段(优先使用这些字段而非解析默认文本输出):--json、issueType、parent、subIssues、subIssuesSummary、blockedBy。blocking、subIssues和blockedBy是结构为blocking的对象(非扁平数组),且{"nodes": [...], "totalCount": N}有数量限制(nodes上限为100,subIssues/blockedBy上限为50),因此需将节点数量与blocking对比以检测截断情况。totalCount - GHES:议题类型和子议题功能需要3.17+版本;被阻塞/阻塞关联关系需要3.19+版本。
Discussions (gh discussion
)
gh discussion讨论功能(gh discussion
)
gh discussionPreview command set, subject to change. Subcommands:
- lists a repo's discussions.
gh discussion list [--state open|closed|all] [--category <name>] [--author <handle>] [--label <name>,...] [--answered] [--search <query>] [--sort created|updated] [--order asc|desc] [--limit N] [--after <cursor>] [--json <fields>] [--web]defaults to open,--stateto updated,--sortto desc.--orderis tri-state (--answeredfor unanswered) for Q&A categories.--answered=false - shows a discussion's body; add
gh discussion view {<number>|<url>|<comment-id>|<comment-url>} [--comments] [--order oldest|newest] [--limit N] [--after <cursor>] [--json <fields>] [--web]for its comments, or pass a comment ID/URL as the argument to list that comment's replies (no--commentsflag;--repliesis rejected with a comment argument).--comments(default newest),--order, and--limitapply only to comment and reply listings.--after - creates a discussion.
gh discussion create [--title <t>] [--body <b> | --body-file <path>] [--category <name>] [--label <name>,...], a body (--titleor--body), and--body-fileare required non-interactively; omitting any will prompt on a terminal.--category - edits title, body, category, or labels.
gh discussion edit {<number>|<url>} [--title <t>] [--body <b>] [--body-file <path>] [--category <name>] [--add-label <name>,...] [--remove-label <name>,...] - adds a top-level comment (when given a discussion) or a reply (when given a comment);
gh discussion comment {<number>|<discussion-url>|<comment-id>|<comment-url>} [--body <b>] [--body-file <path>] [--edit] [--delete] [--yes]or--editupdates or removes a comment/reply and needs a comment ID or URL.--deleteskips the--yesconfirmation.--delete - /
--json/--jqare available on--templateandlistonly;viewandcreateprint the discussion URL.editprints the discussion comment (or reply) URL.comment
预览版命令集,可能会发生变更。子命令包括:
- 列出仓库的讨论内容。
gh discussion list [--state open|closed|all] [--category <name>] [--author <handle>] [--label <name>,...] [--answered] [--search <query>] [--sort created|updated] [--order asc|desc] [--limit N] [--after <cursor>] [--json <fields>] [--web]默认值为open,--state默认值为updated,--sort默认值为desc。--order为三态值(--answered表示未回答),适用于问答类分类。--answered=false - 显示讨论的正文;添加
gh discussion view {<number>|<url>|<comment-id>|<comment-url>} [--comments] [--order oldest|newest] [--limit N] [--after <cursor>] [--json <fields>] [--web]参数可查看评论,或传递评论ID/URL作为参数以列出该评论的回复(无--comments标志;传递评论参数时使用--replies会被拒绝)。--comments(默认值为newest)、--order和--limit仅适用于评论和回复列表。--after - 创建讨论内容。非交互式场景下必须提供
gh discussion create [--title <t>] [--body <b> | --body-file <path>] [--category <name>] [--label <name>,...]、正文(--title或--body)和--body-file;省略任何一项都会在终端中触发输入提示。--category - 编辑讨论的标题、正文、分类或标签。
gh discussion edit {<number>|<url>} [--title <t>] [--body <b>] [--body-file <path>] [--category <name>] [--add-label <name>,...] [--remove-label <name>,...] - 添加顶级评论(传递讨论对象时)或回复(传递评论对象时);
gh discussion comment {<number>|<discussion-url>|<comment-id>|<comment-url>} [--body <b>] [--body-file <path>] [--edit] [--delete] [--yes]或--edit用于更新或删除评论/回复,且需要评论ID或URL。--delete参数可跳过--yes的确认步骤。--delete - /
--json/--jq仅在--template和list命令中可用;view和create命令会打印讨论的URL。edit命令会打印讨论评论(或回复)的URL。comment
Reading files and directories (gh repo read-file
/ read-dir
)
gh repo read-fileread-dir读取文件与目录(gh repo read-file
/ read-dir
)
gh repo read-fileread-dirPreview commands, subject to change. They read a repo's contents over the API
without cloning, and honor () and
(default branch when omitted).
--repo OWNER/REPO-R--ref <branch|tag|commit>- prints a file's contents. In non-TTY contexts the raw bytes go straight to stdout (pipe-friendly); binary files are written as-is when piped but are refused on a TTY. By default, a file containing terminal escape sequences is refused; pass
gh repo read-file <path> [--ref <ref>] [--output <path> [--clobber]] [--allow-escape-sequences] [--json <fields>] [--jq <expr>]to read it anyway.--allow-escape-sequences(--output <path>) writes to disk instead of stdout (a trailing slash writes under a directory using the remote file name;-oallows overwrite); writing to disk always includes the raw bytes regardless of escape sequences.--clobberand--outputare mutually exclusive.--jsonfields include--json,name,path,gitSHA,size,type, andencoding(base64 encoded).content - lists a directory; with no path it lists the repo root. Non-TTY output is tab separated as type, name, octal mode, and byte size.
gh repo read-dir [<path>] [--ref <ref>] [--json <fields>] [--jq <expr>]fields include--json,name,path,type,gitType,mode,modeOctal,gitSHA, andsize. A path pointing at a file errors and points you atsubmodule(and vice versa).read-file
预览版命令,可能会发生变更。这些命令通过API读取仓库内容,无需克隆仓库,且支持()和(省略时默认使用默认分支)参数。
--repo OWNER/REPO-R--ref <branch|tag|commit>- 打印文件内容。在非TTY环境下,原始字节会直接输出到标准输出(便于管道处理);二进制文件在管道传输时会按原样写入,但在TTY环境下会被拒绝读取。默认情况下,包含终端转义序列的文件会被拒绝读取;传递
gh repo read-file <path> [--ref <ref>] [--output <path> [--clobber]] [--allow-escape-sequences] [--json <fields>] [--jq <expr>]参数可强制读取。--allow-escape-sequences(--output <path>)参数可将内容写入磁盘而非标准输出(路径末尾带斜杠时会将文件写入指定目录下,使用远程文件名;-o参数允许覆盖已存在的文件);写入磁盘时始终包含原始字节,不受转义序列影响。--clobber和--output参数互斥。--json支持的字段包括--json、name、path、gitSHA、size、type和encoding(base64编码)。content - 列出目录内容;不指定路径时列出仓库根目录。非TTY环境下的输出以制表符分隔,包含类型、名称、八进制权限和字节大小。
gh repo read-dir [<path>] [--ref <ref>] [--json <fields>] [--jq <expr>]支持的字段包括--json、name、path、type、gitType、mode、modeOctal、gitSHA和size。若指定的路径指向文件,命令会报错并引导你使用submodule(反之亦然)。read-file
Fall back to gh api
for anything --json
doesn't expose
gh api--json对于--json
未暴露的数据,降级使用gh api
--jsongh apiSometimes useful data isn't on the typed commands. Examples:
- Review-thread comments on a PR: (the
gh api repos/{owner}/{repo}/pulls/{n}/commentsflag on--commentsshows issue-level comments only).gh pr view - Arbitrary GraphQL: .
gh api graphql -f query='...' -F var=value - REST shortcuts: - note the
gh api repos/{owner}/{repo}/...placeholder is filled in for you when run from a repo with detected remotes; pass them literally if you want determinism.{owner}/{repo}
有时有用的数据无法通过类型化命令获取。例如:
- PR上的评审线程评论:(
gh api repos/{owner}/{repo}/pulls/{n}/comments的gh pr view标志仅显示议题级别的评论)。--comments - 任意GraphQL查询:。
gh api graphql -f query='...' -F var=value - REST快捷方式:- 注意
gh api repos/{owner}/{repo}/...占位符会在从已检测到远程仓库的目录运行时自动填充;若需要确定性结果,请直接传递字面量。{owner}/{repo}
Authentication
身份验证
- prints the active host(s), user, and which env var (if any) is being honored.
gh auth status - is supported.
gh auth status --json
- 会打印当前活跃的主机、用户以及正在使用的环境变量(如有)。
gh auth status - 参数已被支持。
gh auth status --json
Other notes
其他注意事项
- switches branches. Use
gh pr checkout <n>orgh pr diff <n>if you only need to read.gh pr view <n> - checks the PR out into a git worktree at
gh pr checkout <n> --worktree <path>instead of switching the current branch.<path> - ,
NO_COLOR, andCLICOLOR_FORCEare honored. SetGH_FORCE_TTYif you want TTY-style output (colors, tables, the pager, interactivity) inside an agent harness; leave it unset unless needed.GH_FORCE_TTY=1
- 会切换分支。若你仅需查看内容,使用
gh pr checkout <n>或gh pr diff <n>。gh pr view <n> - 会将PR检出到
gh pr checkout <n> --worktree <path>路径下的git工作区,而非切换当前分支。<path> - 、
NO_COLOR和CLICOLOR_FORCE环境变量会被识别。若你希望在Agent环境中获得TTY风格的输出(颜色、表格、分页器、交互性),设置GH_FORCE_TTY;无需时请勿设置。GH_FORCE_TTY=1