GitHub CLI (gh) 命令行参考手册

版本:2.88.1 | 安装路径:/opt/homebrew/bin/gh

官方地址:https://cli.github.com/ | 安装命令:brew install gh

来源: https://cli.github.com/manual

GitHub CLI(gh)是 GitHub 官方命令行工具,无需离开终端即可管理仓库、Issue、PR、Actions、Release 等,支持脚本自动化与 CI/CD 集成。

安装与配置

安装

参考 安装说明

配置

登录认证

gh auth login

设置偏好编辑器

gh config set editor

设置别名

gh alias set

GitHub Enterprise

gh auth login --hostname

export GH_HOST=

export GHENTERPRISETOKEN=


核心命令

gh auth — 认证管理

子命令说明
gh auth login登录 GitHub 账号
gh auth logout登出 GitHub 账号
gh auth refresh刷新存储的认证凭据
gh auth setup-git配置 git 使用 GitHub CLI
gh auth status显示当前认证状态
gh auth switch切换活跃的 GitHub 账号
gh auth token打印认证 token

#### gh auth login [flags]

-c, --clipboard             复制 OAuth 设备码到剪贴板

-p, --git-protocol string Git 操作协议: {ssh|https}

-h, --hostname string GitHub 实例主机名

--insecure-storage 明文保存认证凭据

-s, --scopes strings 请求额外的认证 scopes

--skip-ssh-key 跳过 SSH 密钥生成/上传提示

-w, --web 在浏览器中打开认证

--with-token 从标准输入读取 token

#### gh auth status [flags]

-a, --active            仅显示活跃账号

-h, --hostname string 仅检查指定主机

-t, --show-token 显示认证 token

#### gh auth token [flags]

-h, --hostname string   GitHub 实例主机名

-u, --user string 指定账号


gh browse — 在浏览器中打开

gh browse [ |  | ] [flags]

-a, --actions                  打开仓库 Actions

--blame 打开文件 blame 视图

-b, --branch string 选择其他分支

-c, --commit string[="last"] 选择指定 commit

-n, --no-browser 仅打印 URL

-p, --projects 打开仓库项目

-r, --releases 打开仓库 Releases

-s, --settings 打开仓库设置

-w, --wiki 打开仓库 Wiki


gh codespace — Codespace 管理

别名: gh cs

子命令说明
gh codespace code在 VS Code 中打开 codespace
gh codespace cp在本地和远程之间复制文件
gh codespace create创建 codespace
gh codespace delete删除 codespace
gh codespace edit编辑 codespace
gh codespace jupyter在 JupyterLab 中打开
gh codespace list列出 codespaces
gh codespace logs查看 codespace 日志
gh codespace ports列出端口
gh codespace ports forward转发端口
gh codespace ports visibility修改端口可见性
gh codespace rebuild重建 codespace
gh codespace sshSSH 进入 codespace
gh codespace stop停止 codespace
gh codespace view查看 codespace 详情


gh gist — Gist 管理

子命令说明
gh gist clone克隆 gist
gh gist create创建 gist (别名: gh gist new)
gh gist delete删除 gist
gh gist edit编辑 gist
gh gist list列出 gists
gh gist rename重命名 gist 文件
gh gist view查看 gist

#### gh gist create [... | -] [flags]

-d, --desc string       Gist 描述

-f, --filename string 从标准输入读取时使用的文件名

-p, --public 公开 gist (默认 "secret")

-w, --web 在浏览器中打开创建的 gist


gh issue — Issue 管理

子命令说明
gh issue close关闭 issue
gh issue comment添加评论
gh issue create创建 issue (别名: gh issue new)
gh issue delete删除 issue
gh issue develop管理关联分支
gh issue edit编辑 issue
gh issue list列出 issues
gh issue lock锁定 issue
gh issue pin置顶 issue
gh issue reopen重新打开 issue
gh issue status显示相关 issues 状态
gh issue transfer转移 issue 到其他仓库
gh issue unlock解锁 issue
gh issue unpin取消置顶
gh issue view查看 issue

#### gh issue create [flags]

-a, --assignee login   指派人员 (使用 "@me" 指派自己)

-b, --body string 提供正文内容

-F, --body-file file 从文件读取正文

-e, --editor 在编辑器中编写

-l, --label name 添加标签

-m, --milestone name 添加到里程碑

-p, --project title 添加到项目

-T, --template name 使用模板

-t, --title string 提供标题

-w, --web 在浏览器中创建

#### gh issue list [flags]

-a, --assignee string   按指派人筛选

-A, --author string 按作者筛选

-l, --label strings 按标签筛选

-L, --limit int 最大数量 (默认 30)

-m, --milestone string 按里程碑筛选

-S, --search query 搜索查询

-s, --state string 按状态: {open|closed|all} (默认 "open")

-w, --web 在浏览器中列出


gh org — 组织管理

子命令说明
gh org list列出已认证用户的组织


gh pr — Pull Request 管理

子命令说明
gh pr checkout检出 PR (别名: gh pr co)
gh pr checks显示 CI 状态
gh pr close关闭 PR
gh pr comment添加评论
gh pr create创建 PR (别名: gh pr new)
gh pr diff查看 PR 变更
gh pr edit编辑 PR
gh pr list列出 PRs
gh pr lock锁定 PR
gh pr merge合并 PR
gh pr ready标记为可审查
gh pr reopen重新打开 PR
gh pr revert回退 PR
gh pr review添加审查
gh pr status显示相关 PRs 状态
gh pr unlock解锁 PR
gh pr update-branch更新 PR 分支
gh pr view查看 PR

#### gh pr create [flags]

-a, --assignee login       指派人员

-B, --base branch 目标分支

-b, --body string PR 正文

-F, --body-file file 从文件读取正文

-d, --draft 标记为草稿

--dry-run 打印详情而不创建

-e, --editor 在编辑器中编写

-f, --fill 使用 commit 信息填充

-H, --head branch 源分支 (默认当前分支)

-l, --label name 添加标签

-m, --milestone name 添加到里程碑

-p, --project title 添加到项目

-r, --reviewer handle 请求审查

-t, --title string PR 标题

-w, --web 在浏览器中创建

#### gh pr merge [] [flags]

    --admin               使用管理员权限合并

--auto 满足要求后自动合并

-d, --delete-branch 合并后删除分支

-m, --merge 使用 merge commit

-r, --rebase 使用 rebase

-s, --squash 使用 squash 合并

#### gh pr list [flags]

-a, --assignee string   按指派人筛选

-A, --author string 按作者筛选

-B, --base string 按 base 分支筛选

-H, --head string 按 head 分支筛选

-l, --label strings 按标签筛选

-L, --limit int 最大数量 (默认 30)

-S, --search query 搜索查询

-s, --state string 按状态: {open|closed|merged|all} (默认 "open")

-w, --web 在浏览器中列出


gh project — GitHub Projects 管理

子命令说明
gh project close关闭项目
gh project copy复制项目
gh project create创建项目
gh project delete删除项目
gh project edit编辑项目
gh project field-create创建字段
gh project field-delete删除字段
gh project field-list列出字段
gh project item-add添加 PR/Issue 到项目
gh project item-archive归档项目项
gh project item-create创建草稿 Issue
gh project item-delete删除项目项
gh project item-edit编辑项目项
gh project item-list列出项目项
gh project link链接项目到仓库/团队
gh project list列出项目
gh project mark-template标记为模板
gh project unlink取消链接
gh project view查看项目


gh release — Release 管理

子命令说明
gh release create创建 release (别名: gh release new)
gh release delete删除 release
gh release delete-asset删除 release 资源
gh release download下载 release 资源
gh release edit编辑 release
gh release list列出 releases
gh release upload上传资源到 release
gh release verify验证 release 签名
gh release verify-asset验证 release 资源
gh release view查看 release

#### gh release create [] [flags]

-d, --draft                        保存为草稿

--generate-notes 自动生成标题和说明

-n, --notes string Release 说明

-F, --notes-file file 从文件读取说明

-p, --prerelease 标记为预发布

--target branch 目标分支或 commit SHA

-t, --title string Release 标题

--verify-tag 验证 tag 是否已存在于远程


gh repo — 仓库管理

子命令说明
gh repo archive归档仓库
gh repo clone克隆仓库
gh repo create创建仓库 (别名: gh repo new)
gh repo delete删除仓库
gh repo edit编辑仓库设置
gh repo forkFork 仓库
gh repo list列出仓库
gh repo rename重命名仓库
gh repo set-default设置默认仓库
gh repo sync同步仓库
gh repo unarchive取消归档
gh repo view查看仓库
gh repo autolink create创建自动链接
gh repo autolink delete删除自动链接
gh repo autolink list列出自动链接
gh repo autolink view查看自动链接
gh repo deploy-key add添加部署密钥
gh repo deploy-key delete删除部署密钥
gh repo deploy-key list列出部署密钥
gh repo gitignore list列出 gitignore 模板
gh repo gitignore view查看 gitignore 模板
gh repo license list列出许可证
gh repo license view查看许可证

#### gh repo create [] [flags]

    --add-readme             添加 README

-c, --clone 克隆到当前目录

-d, --description string 仓库描述

-g, --gitignore string 指定 gitignore 模板

-l, --license string 指定开源许可证

--private 创建私有仓库

--public 创建公开仓库

-p, --template repository 基于模板创建


GitHub Actions 命令

gh cache — Actions 缓存管理

子命令说明
gh cache delete删除缓存
gh cache list列出缓存


gh run — Workflow 运行管理

子命令说明
gh run cancel取消运行
gh run delete删除运行
gh run download下载产物
gh run list列出运行
gh run rerun重新运行
gh run view查看运行
gh run watch监控运行进度

#### gh run list [flags]

-b, --branch string     按分支筛选

-c, --commit SHA 按 commit SHA 筛选

-e, --event event 按触发事件筛选

-L, --limit int 最大数量 (默认 20)

-s, --status string 按状态筛选

-w, --workflow string 按 workflow 筛选

#### gh run view [] [flags]

-j, --job string    查看特定 job

--log 查看完整日志

--log-failed 查看失败步骤日志

-v, --verbose 显示 job 步骤

-w, --web 在浏览器中打开


gh workflow — Workflow 管理

子命令说明
gh workflow disable禁用 workflow
gh workflow enable启用 workflow
gh workflow list列出 workflows
gh workflow run触发 workflow
gh workflow view查看 workflow

#### gh workflow run [ | ] [flags]

-F, --field key=value       添加参数 (支持 @ 语法)

-f, --raw-field key=value 添加字符串参数

-r, --ref string 指定分支或标签


附加命令

gh agent-task — Agent 任务管理 (预览)

别名: gh agent-tasks, gh agent, gh agents

子命令说明
gh agent-task create创建 agent 任务
gh agent-task list列出 agent 任务
gh agent-task view查看 agent 任务


gh alias — 命令别名

子命令说明
gh alias delete删除别名
gh alias import从 YAML 文件导入别名
gh alias list列出别名
gh alias set创建别名

#### gh alias set [flags]

    --clobber   覆盖已有同名别名

-s, --shell 声明为 shell 别名


gh api [flags] — GitHub API 请求

    --cache duration        缓存响应, 如 "3600s", "60m", "1h"

-F, --field key=value 添加类型化参数

-H, --header key:value 添加 HTTP 请求头

--hostname string GitHub 主机名 (默认 "github.com")

-i, --include 包含 HTTP 响应状态和头

--input file 请求体文件

-q, --jq string 使用 jq 语法查询

-X, --method string HTTP 方法 (默认 "GET")

--paginate 获取所有分页结果

-p, --preview strings 启用 API 预览

-f, --raw-field key=value 添加字符串参数

--silent 不打印响应体

--slurp 配合 --paginate 返回所有页面的数组

-t, --template string 使用 Go 模板格式化

--verbose 包含完整的 HTTP 请求和响应


gh attestation — 制品证明管理

别名: gh at

子命令说明
gh attestation download下载制品证明
gh attestation trusted-root输出可信根
gh attestation verify验证制品完整性


gh completion -s — Shell 补全脚本

-s, --shell string   Shell 类型: {bash|zsh|fish|powershell}


gh config — 配置管理

子命令说明
gh config clear-cache清除 CLI 缓存
gh config get 获取配置值
gh config list列出配置
gh config set 设置配置值


gh copilot — GitHub Copilot CLI (预览)

--remove   删除已下载的 Copilot CLI


gh extension — 扩展管理

别名: gh extensions, gh ext

子命令说明
gh extension browse浏览扩展 UI
gh extension create创建扩展
gh extension exec执行已安装的扩展
gh extension install安装扩展
gh extension list列出已安装的扩展
gh extension remove移除扩展
gh extension search搜索扩展
gh extension upgrade升级扩展


gh gpg-key — GPG 密钥管理

子命令说明
gh gpg-key add添加 GPG 密钥
gh gpg-key delete删除 GPG 密钥
gh gpg-key list列出 GPG 密钥


gh label — 标签管理

子命令说明
gh label clone从其他仓库克隆标签
gh label create创建标签
gh label delete删除标签
gh label edit编辑标签
gh label list列出标签


gh licenses — 查看第三方许可证信息


gh ruleset — 仓库规则集

别名: gh rs

子命令说明
gh ruleset check查看适用于分支的规则
gh ruleset list列出规则集
gh ruleset view查看规则集详情


gh search — 搜索

子命令说明
gh search code搜索代码
gh search commits搜索 commits
gh search issues搜索 issues
gh search prs搜索 PRs
gh search repos搜索仓库

#### gh search repos [] [flags]

    --language string     按语言筛选

--license strings 按许可证筛选

-L, --limit int 最大数量 (默认 30)

--sort string 排序: {forks|help-wanted-issues|stars|updated}

--stars number 按星标数筛选

--topic strings 按 topic 筛选

-w, --web 在浏览器中搜索


gh secret — Secret 管理

子命令说明
gh secret delete删除 secret
gh secret list列出 secrets
gh secret set创建或更新 secret

#### gh secret set [flags]

-a, --app string           应用类型: {actions|codespaces|dependabot}

-b, --body string Secret 值 (不指定则从标准输入读取)

-e, --env environment 环境级 secret

-o, --org organization 组织级 secret

-v, --visibility string 可见性: {all|private|selected} (默认 "private")


gh ssh-key — SSH 密钥管理

子命令说明
gh ssh-key add添加 SSH 密钥
gh ssh-key delete删除 SSH 密钥
gh ssh-key list列出 SSH 密钥


gh status — 状态概览

gh status [flags]

-e, --exclude strings   排除的仓库列表 (owner/name 格式)

-o, --org string 在组织范围内报告状态


gh variable — Actions 变量管理

子命令说明
gh variable delete删除变量
gh variable get获取变量
gh variable list列出变量
gh variable set创建或更新变量

#### gh variable set [flags]

-b, --body string          变量值

-e, --env environment 环境级变量

-o, --org organization 组织级变量

-v, --visibility string 可见性: {all|private|selected} (默认 "private")


环境变量

变量说明
GITHUB_TOKEN认证 token (替代 gh auth login)
GH_HOST默认 GitHub 主机名
GHENTERPRISETOKENGitHub Enterprise token
GH_REPO默认仓库 (格式: owner/repo)

退出码

退出码说明
0成功
1一般错误
2shell 用法错误 (参数不正确)

通用 JSON 输出选项

以下选项在多个命令中通用:

-q, --jq expression     使用 jq 表达式过滤 JSON 输出

--json fields 指定字段输出 JSON

-t, --template string 使用 Go 模板格式化 JSON


完整文档: https://cli.github.com/manual