gh cli 使用
GitHub CLI (gh) 命令行工具常用操作速查。
# 安装
# macOS
brew install gh
# Ubuntu / Debian
sudo apt install gh
# CentOS / RHEL
sudo dnf install gh
# Windows
winget install GitHub.cli
# 认证
# 交互式登录(浏览器授权)
gh auth login
# 用 token 登录
gh auth login --with-token < token.txt
# 查看当前认证状态
gh auth status
# 登出
gh auth logout
# 仓库操作
# 克隆仓库
gh repo clone owner/repo
# 在浏览器中打开仓库
gh repo view owner/repo --web
# 查看仓库信息
gh repo view owner/repo
# fork 仓库
gh repo fork owner/repo
# 创建新仓库
gh repo create my-project --public --clone
gh repo create my-project --private
# Pull Request
# 创建 PR(交互式)
gh pr create
# 快速创建 PR
gh pr create --title "feat: add login" --body "实现登录功能"
# 起草 PR
gh pr create --title "WIP: refactor" --draft
# 指定 reviewer 和 label
gh pr create --title "fix: bug" --reviewer user1,user2 --label bug
# 列出 PR
gh pr list
# 列出自己的 PR
gh pr list --author @me
# 查看 PR 详情
gh pr view 123
# 在浏览器中查看 PR
gh pr view 123 --web
# 检出别人的 PR 到本地
gh pr checkout 123
# 合并 PR
gh pr merge 123 --merge
gh pr merge 123 --squash
gh pr merge 123 --rebase
# 关闭 PR
gh pr close 123
# Issue
# 创建 issue
gh issue create --title "Bug: 登录失败" --body "描述内容"
# 交互式创建
gh issue create
# 列出 issue
gh issue list
# 按状态筛选
gh issue list --state open
gh issue list --state closed
# 按标签筛选
gh issue list --label bug
# 查看 issue 详情
gh issue view 456
# 关闭 issue
gh issue close 456
# 重新打开 issue
gh issue reopen 456
# Actions (CI/CD)
# 列出 workflow 运行记录
gh run list
# 查看某次运行详情
gh run view <run-id>
# 实时查看运行日志
gh run watch <run-id>
# 重新运行失败的 workflow
gh run rerun <run-id> --failed
# 列出仓库的 workflows
gh workflow list
# 手动触发 workflow
gh workflow run <workflow-name>
gh workflow run <workflow-name> --field param1=value1
# 查看 workflow 运行日志
gh run view <run-id> --log
# Release
# 创建 release
gh release create v1.0.0 --title "v1.0.0" --notes "首个正式版本"
# 上传附件
gh release create v1.0.0 ./dist/*.tar.gz --title "v1.0.0"
# 从 tag 创建 draft release
gh release create v1.0.0 --draft --title "v1.0.0"
# 列出所有 release
gh release list
# 下载 release 资源
gh release download v1.0.0
# 下载到指定目录
gh release download v1.0.0 -D ./downloads
# 删除 release
gh release delete v1.0.0
# Gist
# 创建 gist
gh gist create file.txt
# 创建私有 gist
gh gist create file.txt --private
# 列出 gist
gh gist list
# 查看 gist 内容
gh gist view <gist-id>
# 编辑 gist
gh gist edit <gist-id>
# 删除 gist
gh gist delete <gist-id>
# API 调用
# 直接调用 GitHub REST API
gh api repos/owner/repo/releases
# GET 请求
gh api /repos/owner/repo/pulls --method GET
# POST 请求
gh api /repos/owner/repo/issues \
--method POST \
-f title="Bug report" \
-f body="描述内容"
# 使用 jq 过滤结果
gh api repos/owner/repo/issues --jq '.[].title'
# 通用技巧
# 查看帮助
gh help
gh pr help
# 所有命令都支持 --web 在浏览器中打开
gh repo view --web
gh pr view 123 --web
# 设置默认编辑器
gh config set editor vim
# 查看所有配置
gh config list
上次更新: 2026/05/31, 12:38:24