GitHub CLI 命令行工具

精选 GitHub CLI 命令行工具 常用指令与核心速查备忘单,涵盖高频用法、配置参数与实用技巧。

#入门指南

#安装 (Installation)

Windows

Tool Install Upgrade
WinGet winget install --id Github.cli winget upgrade --id GitHub.cli
Scoop scoop install gh scoop update gh
Choco choco install gh choco upgrade gh

Mac OS

Tool Install Upgrade
Brew brew install gh brew upgrade gh
MacPorts sudo port install gh sudo port selfupdate && sudo port upgrade gh

Linux

See the install instructions for other Linux distributions.

Installation Script:

type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y

#入门指南

#安装 (Installation)

Windows

Tool Install Upgrade
WinGet winget install --id Github.cli winget upgrade --id GitHub.cli
Scoop scoop install gh scoop update gh
Choco choco install gh choco upgrade gh

Mac OS

Tool Install Upgrade
Brew brew install gh brew upgrade gh
MacPorts sudo port install gh sudo port selfupdate && sudo port upgrade gh

Linux

See the install instructions for other Linux distributions.

Installation Script:

type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y

#入门指南

#安装 (Installation)

Windows

Tool Install Upgrade
WinGet winget install --id Github.cli winget upgrade --id GitHub.cli
Scoop scoop install gh scoop update gh
Choco choco install gh choco upgrade gh

Mac OS

Tool Install Upgrade
Brew brew install gh brew upgrade gh
MacPorts sudo port install gh sudo port selfupdate && sudo port upgrade gh

Linux

See the install instructions for other Linux distributions.

Installation Script:

type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y

#入门指南

#安装 (Installation)

Windows

Tool Install Upgrade
WinGet winget install --id Github.cli winget upgrade --id GitHub.cli
Scoop scoop install gh scoop update gh
Choco choco install gh choco upgrade gh

Mac OS

Tool Install Upgrade
Brew brew install gh brew upgrade gh
MacPorts sudo port install gh sudo port selfupdate && sudo port upgrade gh

Linux

See the install instructions for other Linux distributions.

Installation Script:

type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y

#入门指南

#安装 (Installation)

Windows

Tool Install Upgrade
WinGet winget install --id Github.cli winget upgrade --id GitHub.cli
Scoop scoop install gh scoop update gh
Choco choco install gh choco upgrade gh

Mac OS

Tool Install Upgrade
Brew brew install gh brew upgrade gh
MacPorts sudo port install gh sudo port selfupdate && sudo port upgrade gh

Linux

See the install instructions for other Linux distributions.

Installation Script:

type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y

#入门指南

#安装 (Installation)

Windows

Tool Install Upgrade
WinGet winget install --id Github.cli winget upgrade --id GitHub.cli
Scoop scoop install gh scoop update gh
Choco choco install gh choco upgrade gh

Mac OS

Tool Install Upgrade
Brew brew install gh brew upgrade gh
MacPorts sudo port install gh sudo port selfupdate && sudo port upgrade gh

Linux

See the install instructions for other Linux distributions.

Installation Script:

type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y

#入门指南

#安装 (Installation)

Windows

Tool Install Upgrade
WinGet winget install --id Github.cli winget upgrade --id GitHub.cli
Scoop scoop install gh scoop update gh
Choco choco install gh choco upgrade gh

Mac OS

Tool Install Upgrade
Brew brew install gh brew upgrade gh
MacPorts sudo port install gh sudo port selfupdate && sudo port upgrade gh

Linux

See the install instructions for other Linux distributions.

Installation Script:

type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y

#入门指南

#安装 (Installation)

Windows

Tool Install Upgrade
WinGet winget install --id Github.cli winget upgrade --id GitHub.cli
Scoop scoop install gh scoop update gh
Choco choco install gh choco upgrade gh

Mac OS

Tool Install Upgrade
Brew brew install gh brew upgrade gh
MacPorts sudo port install gh sudo port selfupdate && sudo port upgrade gh

Linux

See the install instructions for other Linux distributions.

Installation Script:

type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y)
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
&& sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
&& sudo apt update \
&& sudo apt install gh -y

#基础用法

帮助与文档菜单

显示命令选项:

命令指令 含义说明
gh help [command] 提供应用中任意命令的帮助说明。直接输入 gh help [命令路径] 即可查看详细信息。

身份认证 (Authentication)

命令指令 含义说明
gh auth login 使用默认的 Web 浏览器进行登录认证
gh auth logout 清除指定主机的登录认证配置信息
gh auth refresh 扩展或修复已保存凭据的权限范围 (Scopes)
gh auth setup-git 配置 git 将 GitHub CLI 作为凭据管理器 (credential helper)
gh auth status 验证并显示当前的认证登录状态信息
gh auth token 打印当前 gh 配置使用的 Auth Token

使用 Github Token 直接登录:

$ gh auth --with-token < token.txt

#仓库管理 (Repository Management)

常规仓库命令

命令指令 含义说明
gh repo create 创建一个新的 GitHub 仓库
gh repo list [target] 列出指定用户或组织拥有的仓库列表

目标仓库操作

命令指令 含义说明
gh repo archive [repo] 归档存档指定 GitHub 仓库
gh repo clone [dir] 将 GitHub 仓库克隆至本地
gh repo delete [repo] 删除指定 GitHub 仓库
gh repo deploy-key 管理仓库的部署密钥 (Deploy Keys)
gh repo edit [repo] 编辑修改仓库设置选项
gh repo fork [repo] Fork 分叉指定 GitHub 仓库
gh repo rename [name] 重命名 GitHub 仓库
gh repo set-default [repo] 设置默认远程关联仓库
gh repo sync [dest-repo] 从源仓库同步更新至目标仓库
gh repo view [repo] 查看展示 GitHub 仓库的描述与 README 内容

#Issues 问题追踪

搜索 Issues

命令指令 含义说明
gh search issues [query] 在 GitHub 上搜索 Issue 问题

高频示例

# 搜索包含关键字 "readme" 和 "typo" 的 issues
$ gh search issues readme typo

# 搜索匹配精确短语 "broken feature" 的 issues
$ gh search issues "broken feature"

# 在 cli 组织中搜索 issues 和 pull requests
$ gh search issues --include-prs --owner=cli

# 搜索分配指派给自己的开启中 (open) issues
$ gh search issues --assignee=@me --state=open

# 搜索评论数超过 100 条的 issues
$ gh search issues --comments=">100"

# 搜索不带有 "bug" 标签的 issues
$ gh search issues -- -label:bug

#Pull Requests 合并请求

PR 操作指令

命令指令 含义说明
gh pr create 在 GitHub 上创建 Pull Request
gh pr list 列出 GitHub 仓库中的 Pull Request
gh pr status 显示相关 Pull Request 的最新状态

使用示例

$ gh pr status

输出示例:

Current branch
  #12 Remove the test feature [user:patch-2]
   - All checks failing - Review 必填

Created by you
  You have no open pull requests

Requesting a code review from you
  #13 Fix tests [branch]
  - 3/4 checks failing - Review 必填
  #15 New feature [branch]
   - Checks passing - Approved

#GitHub Actions 工作流

常规工作流操作

命令指令 含义说明
gh workflow disable 禁用指定工作流,防止其运行或在列表中显示
gh workflow enable 启用指定工作流,允许运行并在列表中显示
gh workflow list 列出工作流文件,默认隐藏已禁用的工作流
gh workflow run 为指定工作流触发 workflow_dispatch 事件
gh workflow view 查看工作流概览摘要

运行记录管理

命令指令 含义说明
gh run cancel 取消正在运行的工作流
gh run delete 删除指定的工作流运行记录
gh run download 下载 GitHub Actions 运行产生的产物文件 (Artifacts)
gh run list 列出最近的工作流运行记录
gh run rerun 重新运行整个工作流、仅重试失败 Job,或重试指定 Job
gh run view 查看工作流运行的详细摘要信息
gh run watch 实时监控工作流运行状态直至其完成

#命令别名 (Aliases)

别名配置管理

命令指令 含义说明
gh alias delete 删除设置的命令别名
gh alias import 从 YAML 文件导入命令别名
gh alias list 打印目前 gh 已配置的所有命令别名
gh alias set 定义一个别名,调用时将展开为完整的 gh 命令

#发布版本 (Releases)

常规发布操作

命令指令 含义说明
gh release create 为仓库创建一个新的 GitHub Release 发布版本
gh release list 列出仓库中的 Release 版本列表

发布版本细分操作

命令指令 含义说明
gh release delete 删除指定的 Release 版本
gh release delete-asset 从 Release 版本中删除指定的资产文件 (Asset)
gh release download 下载 GitHub Release 发布的资产文件
gh release edit 编辑修改 Release 发布版本
gh release upload 上传资产文件至 GitHub Release
gh release view 查看 GitHub Release 发布的详细信息

#全局配置 (Configuration)

配置项管理

命令指令 含义说明
gh config clear-cache 清理 CLI 缓存数据
gh config get 打印指定配置键 (Key) 的取值
gh config list 打印配置键与取值的完整列表
gh config set 更新指定配置键的值