| name | github |
| description | Interact with GitHub using the `gh` CLI. Use `gh issue`, `gh pr`, `gh run`, and `gh api` for issues, PRs, CI runs, and advanced queries. |
| triggers | ["GitHub","github","代码仓库","开源","pull request","issue","仓库","代码","CI/CD","工作流"] |
GitHub Skill
使用 gh CLI 与 GitHub 交互。当不在 git 目录中时,始终指定 --repo owner/repo,或直接使用 URL。
🚀 快速开始
基本命令
gh repo view owner/repo
gh repo clone owner/repo
gh repo create my-new-repo --public --clone
身份验证
gh auth login
gh auth status
📝 问题 (Issues)
创建和管理问题
gh issue create --title "Bug报告" --body "详细描述..." --repo owner/repo
gh issue list --repo owner/repo --state all
gh issue view 123 --repo owner/repo
gh issue comment 123 --body "这是修复建议" --repo owner/repo
问题搜索和过滤
gh issue list --repo owner/repo --label bug
gh issue list --repo owner/repo --state open
gh issue list --repo owner/repo --author username
gh issue list --repo owner/repo --assignee username
🔀 拉取请求 (Pull Requests)
创建和管理 PR
gh pr create --title "新功能" --body "详细说明..." --repo owner/repo
gh pr view 55 --repo owner/repo
gh pr list --repo owner/repo --state all
gh pr merge 55 --repo owner/repo --squash
gh pr checks 55 --repo owner/repo
PR 审查和评论
gh pr comment 55 --body "很好的修改!" --repo owner/repo
gh pr review 55 --repo owner/repo --request-changes --body "需要修复..."
gh pr review 55 --repo owner/repo --approve --body "已批准"
🏃 持续集成 (CI/CD)
查看工作流运行
gh run list --repo owner/repo
gh run view <run-id> --repo owner/repo
gh run view <run-id> --repo owner/repo --log-failed
gh run rerun <run-id> --repo owner/repo
工作流状态
gh run list --repo owner/repo --limit 10
gh run list --repo owner/repo --status failure
gh run list --repo owner/repo --branch main
🛠️ 仓库管理
分支操作
gh repo create-branch new-feature --base main --repo owner/repo
gh repo checkout feature-branch --repo owner/repo
gh repo delete-branch old-feature --repo owner/repo
发布管理
gh release create v1.0.0 --title "版本1.0.0" --notes "新功能..." --repo owner/repo
gh release list --repo owner/repo
gh release view v1.0.0 --repo owner/repo
🔧 高级查询 (gh api)
gh api 命令用于访问其他子命令无法获取的数据。
基本 API 调用
gh api repos/owner/repo
gh api repos/owner/repo/pulls/55 --jq '.title, .state, .user.login'
gh api repos/owner/repo/issues/123 --jq '.title, .body, .labels[].name'
使用 jq 处理 JSON
gh api user/repos --jq '.[] | select(.private == false) | .full_name'
gh api repos/owner/repo/commits --jq '.[0] | .commit.message, .author.login'
gh api repos/owner/repo/issues --jq 'length'
📊 JSON 输出
大多数命令支持 --json 用于结构化输出。可以使用 --jq 进行过滤:
gh issue list --repo owner/repo --json number,title,state,createdAt --jq '.[] | "\(.number): \(.title) [\(.state)]"'
gh pr list --repo owner/repo --json number,title,author,state --jq '.[] | "PR #\(.number): \(.title) by \(.author.login)"'
gh run list --repo owner/repo --json databaseId,workflowName,status,conclusion --jq '.[] | "\(.databaseId): \(.workflowName) - \(.status)"'
🎯 实用示例
示例 1:检查未合并的 PR
gh pr list --repo owner/repo --state open --json number,title,createdAt --jq '.[] | "PR #\(.number): \(.title) (创建于: \(.createdAt))"'
示例 2:统计问题
gh issue list --repo owner/repo --state open --json number | jq length
gh issue list --repo owner/repo --state all --json labels | jq -r '.[].labels[].name' | sort | uniq -c
示例 3:自动化工作流
gh issue create --repo owner/repo --title "Bug: $BUG_TITLE" --body-file bug_template.md
gh issue list --repo owner/repo --state open --json number,createdAt --jq '.[] | select(.createdAt < "2024-01-01") | .number' | xargs -I {} gh issue close {} --repo owner/repo
⚙️ 配置和提示
环境变量
export GH_REPO=owner/repo
export GH_OUTPUT=json
实用提示
- 使用
--repo 参数:当不在 git 目录时,始终指定仓库
- 善用
--json 和 --jq:用于脚本和自动化
- 使用
--web 打开网页:gh pr view 55 --web 在浏览器中打开
- 使用分页:
--limit 100 获取更多结果
- 利用别名:创建常用命令的别名
常见问题
Q: 如何在没有 git 目录的情况下使用 gh?
A: 始终使用 --repo owner/repo 参数指定仓库。
Q: 如何批量处理问题?
A: 结合 gh issue list --json 和 jq 进行过滤,然后使用 xargs 处理。
Q: 如何查看命令的完整选项?
A: 使用 gh issue --help 查看特定命令的帮助信息。
📚 学习资源
gh help
gh issue --help
gh pr --help
gh --version
GitHub CLI 文档:https://cli.github.com/manual/
GitHub REST API 文档:https://docs.github.com/en/rest