GitHub CLI(gh)是 GitHub 官方出品的命令行工具,让你不用打开浏览器就能完成绝大多数 GitHub 操作——创建仓库、提交 PR、管理 Issue、查看 CI 运行结果等,全部在终端搞定。它的核心价值在于:用 gh auth login 一次浏览器授权替代了以前繁琐的 SSH 密钥配置,同时把 GitHub 的工作流无缝嵌入到你的终端开发流程中。对于需要向开源项目提交代码、管理多个仓库、或者在服务器上自动化 GitHub 操作的开发者来说,gh 是一个值得上手的效率工具。
gh 指令:
bash展开代码gh auth status # 查看当前登录状态
gh repo list --limit 20 # 列出自己的仓库
gh pr create \
--repo modelscope/ms-swift \ # 目标上游仓库
--head xxddccaa:feat/xxx \ # 来源:fork用户:分支名
--base main \ # 目标分支
--title 'PR标题' \
--body '正文内容' # 创建 PR
下面是我整理的 gh 常用场景速查:
Ubuntu / Debian
bash展开代码sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg \
| sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/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
CentOS / RHEL / Fedora
bash展开代码sudo dnf install 'dnf-command(config-manager)'
sudo dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo
sudo dnf install gh
macOS
bash展开代码brew install gh
验证安装
bash展开代码gh --version
方式一:浏览器授权(推荐,最简单)
bash展开代码gh auth login
按提示选择:
GitHub.com(公有云)或 GitHub Enterprise(私有部署)HTTPSYes浏览器会打开 GitHub 授权页,点击授权后自动完成,终端会显示 Logged in。
方式二:用 Token 登录(服务器无浏览器场景)
先去 GitHub 网页手动生成 Token:
Settings → Developer settings → Personal access tokens → Tokens (classic) → Generate new token
勾选权限:repo、workflow、read:org(按需)
然后在终端:
bash展开代码gh auth login
# 选择 HTTPS,最后选 "Paste an authentication token"
# 粘贴刚才生成的 token
或者直接用环境变量(CI 场景常用):
bash展开代码export GH_TOKEN=your_token_here
gh auth status # 验证是否生效
验证登录状态
bash展开代码gh auth status
输出示例:
展开代码github.com ✓ Logged in to github.com as xxddccaa ✓ Git operations for github.com configured to use https protocol. ✓ Token: *******************
bash展开代码# 列出自己的仓库
gh repo list
gh repo list --limit 50
# 创建新仓库
gh repo create my-repo --public
gh repo create my-repo --private --description "描述"
# Clone 仓库(自动处理认证)
gh repo clone owner/repo
# Fork 一个仓库
gh repo fork owner/repo
gh repo fork owner/repo --clone # fork 并立即 clone 到本地
# 查看仓库信息
gh repo view owner/repo
gh repo view owner/repo --web # 在浏览器打开
bash展开代码# 列出 issue
gh issue list
gh issue list --repo owner/repo
gh issue list --state closed
gh issue list --label bug
# 创建 issue
gh issue create --title "Bug标题" --body "描述"
# 查看某个 issue
gh issue view 123
gh issue view 123 --web
# 关闭 / 重开 issue
gh issue close 123
gh issue reopen 123
# 给自己分配 issue
gh issue edit 123 --add-assignee @me
bash展开代码# 列出 PR
gh pr list
gh pr list --repo owner/repo
gh pr list --state merged
# 创建 PR(当前分支 → 默认主分支)
gh pr create
gh pr create --title "标题" --body "描述"
gh pr create --draft # 草稿 PR
# 向上游 fork 提交 PR(fork 场景)
gh pr create \
--repo upstream-owner/repo \
--head your-username:your-branch \
--base main \
--title "标题" \
--body "描述"
# 查看 PR
gh pr view 123
gh pr view 123 --web
# checkout 到某个 PR 的分支(review 用)
gh pr checkout 123
# 合并 PR
gh pr merge 123
gh pr merge 123 --squash
gh pr merge 123 --rebase
# 关闭 PR
gh pr close 123
bash展开代码# 查看 workflow 运行列表
gh run list
# 查看某次运行的详情和日志
gh run view 1234567
gh run view 1234567 --log
# 手动触发 workflow
gh workflow run ci.yml
# 查看所有 workflow
gh workflow list
bash展开代码# 列出 release
gh release list
# 创建 release
gh release create v1.0.0
gh release create v1.0.0 --title "v1.0.0" --notes "更新内容"
# 上传附件到 release
gh release upload v1.0.0 ./dist/app.tar.gz
# 下载 release 的附件
gh release download v1.0.0
bash展开代码# 在浏览器打开当前仓库
gh browse
# 查看通知
gh notification list
# 搜索仓库
gh search repos "关键词" --limit 10
# 用 gh 直接调用 GitHub API(高级用法)
gh api repos/owner/repo
gh api repos/owner/repo/pulls --jq '.[].title'


本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!