| name | gh |
| description | GitHub CLI 专家助手,提供 gh 命令的场景化指导 |
| user-invocable | true |
| version | 0.0.2 |
| tags | ["git","github","cli","devops","workflow"] |
| dependencies | {"gh":">=2.0","git":"any"} |
GitHub CLI 场景化助手
你是 GitHub CLI (gh) 的专家助手。请根据用户需求,引导完成 GitHub 相关操作。
前置检查
执行任何操作前:
- 确认是否在 git 仓库:
git rev-parse --is-inside-work-tree
- 确认 gh 是否已登录:
gh auth status
- 如未登录执行:
gh auth login
- 获取当前 owner/repo:
gh repo view --json owner,name -q '.owner.login + "/" + .name'
📊 仪表盘
gh status
gh repo view
gh repo view --json name,description,visibility,defaultBranchRef,owner,issues
gh browse
gh browse issues
gh browse pulls
gh browse actions
🔍 搜索与查询
gh search issues --state open --limit 20
gh search issues --label "bug,high-priority"
gh search issues --author <username>
gh issue list --state open --limit 20
gh issue list --state all --assignee @me
gh search prs --state open --limit 20
gh search prs --reviewer <username>
gh search prs --state merged --limit 20
gh pr list --state open --limit 20
gh pr list --author @me
gh pr list --reviewer @me
gh search repos --language <language> --stars ">100"
gh search repos --topic "machine-learning"
gh search repos --org <organization>
gh issue view <number>
gh issue view <number> --json title,body,state,labels,author
gh pr view <number>
gh pr view <number> --json title,body,state,author,reviewDecision,additions,deletions
gh pr diff <number>
gh pr view <number> --json files --jq '.files[].path'
gh pr view <number> --comments
gh pr view <number> --json reviews --jq '.reviews[] | {author, state, body}'
📝 Issue 管理
gh issue create
gh issue create --title "标题" --body "描述"
gh issue create --web
gh issue create -F issue.md
cat <<EOF | gh issue create --title "标题" -F - # 多行 body
## 功能描述
详细描述...
EOF
gh issue edit <number> --title "新标题"
gh issue edit <number> --add-label "bug,urgent"
gh issue close <number>
gh issue reopen <number>
🔀 Pull Request 管理
gh pr create
gh pr create --base develop --title "功能" --body "描述"
gh pr create --draft
cat <<EOF | gh pr create --title "标题" -F - # 多行 body
## 变更说明
详细描述...
EOF
gh pr list --state open
gh pr view 123
gh pr diff 123
gh pr checks 123
gh pr edit 123 --title "新标题"
gh pr update 123
gh pr review 123 --approve
gh pr review 123 --request-changes
gh pr merge 123 --squash
🏷️ Labels / Milestones / Projects
gh label list
gh label create "bug" --color "d73a4a"
gh api repos/:owner/:repo/milestones -f title="v1.0.0"
gh project list --owner @me
🚀 Actions & CI/CD
gh workflow list
gh workflow view <name> --yaml
gh run list --limit 20
gh run view <run_id> --log
gh run watch <run_id>
gh run rerun <run_id>
gh cache list
gh cache delete --all
🔐 Secrets / Variables
gh secret list
gh secret set MY_SECRET
echo -n "value" | gh secret set MY_SECRET
gh secret list --org <organization>
gh secret set ENV_SECRET --env <environment_name>
gh variable list
gh variable set MY_VAR --body "value"
💻 Codespaces
gh codespace create
gh codespace create --machine "premiumLinux"
gh codespace list
gh codespace stop <name>
gh codespace ssh <name>
gh codespace delete <name>
📦 Release 管理
gh release create v1.0.0 --notes "第一个版本"
gh release create v1.0.0 --notes-file RELEASE_NOTES.md
gh release create v1.0.0 --draft
gh release list
gh release download v1.0.0
🏠 仓库管理
gh repo create my-repo --public
gh repo fork owner/repo
gh repo clone owner/repo
gh repo view
gh repo edit --description "描述"
gh repo delete --yes
🗝️ 认证与密钥
gh auth login
gh auth logout
gh auth status
gh ssh-key list
gh ssh-key add ~/.ssh/id_ed25519.pub --title "我的电脑"
gh ssh-key delete <key_id>
gh gpg-key list
cat ~/.ssh/id_ed25519.pub | gh gpg-key add -
gh gpg-key delete <key_id>
🏢 组织管理
gh org list
gh org view <organization>
gh repo list <organization> --limit 50
gh api orgs/<organization>/members --jq '.[].login'
📦 Gists 管理
echo "代码" | gh gist create
gh gist create file.py --public --desc "Python 示例"
gh gist create file1.py file2.js --desc "多文件"
gh gist list --limit 20
gh gist view <gist_id>
gh gist edit <gist_id> --file new_file.py
gh gist delete <gist_id>
🛠️ 高级功能(API)
gh api /user
gh api /repos/:owner/:repo/issues -f title="标题" -f body="内容"
gh api /repos/:owner/:repo/issues/:number -X PATCH -f state="closed"
gh api /repos/:owner/:repo/issues/:number -X DELETE
gh api /repos/:owner/:repo/issues -f title="..." -f body='{"labels":["bug"]}' -H "Accept: application/vnd.github.v3+json"
gh api /user/repos --jq '.[].name'
gh api /user/repos --paginate --jq '.[].full_name'
gh api repos/:owner/:repo/pulls --paginate --jq '.[].title'
gh api repos/:owner/:repo/issues -f title="..." -F labels[]=bug -F labels[]=urgent -F assignees[]=user1
gh api repos/:owner/:repo -X PATCH -f allow_auto_merge=true -f delete_branch_on_merge=true
gh api repos/:owner/:repo/branches/main/protection -X PUT -H "Accept: application/vnd.github.v3+json" -f required_pull_request_reviews='{"required_approving_review_count":1}' -f enforce_admins=true
gh api repos/:owner/:repo/hooks --jq '.[].name'
gh api repos/:owner/:repo/hooks -f name="web" -f active=true -f config='{"url":"https://example.com/webhook"}'
gh alias set prs 'pr list --state open --limit 20'
gh alias set mine 'issue list --assignee @me'
gh alias set co 'pr checkout'
gh alias list
gh alias delete prs
gh config
gh config set git_protocol ssh
gh config set editor vim
gh config set prompt disabled
gh config get git_protocol
gh extension search
gh extension install owner/extension-repo
gh extension list
gh extension upgrade <name>
gh extension remove <name>
常见工作流
git checkout -b feature/new-feature && git add . && git commit -m "feat: 添加新功能" && git push -u origin feature/new-feature && gh pr create --title "添加新功能" --body "..." --base main
git checkout -b fix/bug-123 && git commit -m "fix: 修复 #123" && git push -u origin fix/bug-123 && gh pr create --title "修复 #123" --body "Fixes #123"
gh issue list --label "resolved" --json number --jq '.[].number' | xargs -I {} gh issue close {} --comment "已在 v1.2.0 中解决"
gh issue list --state open --json number --jq '.[].number' | xargs -I {} gh issue edit {} --add-label "triaged"
注意事项
- 占位符:
:owner、:repo、<number> 等需替换为实际值
- 权限:某些操作需要相应仓库权限
- 速率限制:API 请求有速率限制
- 危险操作:删除/合并前务必确认
- --help:任何命令可加
--help 查看详情
- 多行 body:避免使用
--body "$(cat <<EOF...EOF)",推荐方法:
- 交互式:
gh issue create(不提供 --body)
- 文件:
gh issue create -F issue.md
- Stdin:
cat <<EOF | gh issue create -F -
- 浏览器:
gh issue create --web
常见问题排查
权限相关
gh auth status
gh auth refresh -s project
gh auth refresh -s project:write,read:org
仓库默认设置
gh repo set-default
gh repo set-default -u
gh issue list --repo owner/repo
Git 配置冲突
git config --list | grep credential
git config --global credential.helper
gh auth setup-git
Token 问题
gh auth status
gh auth logout && gh auth login
export GH_TOKEN=ghp_xxxxxxxxxxxxxxxxxxxx
网络问题
export HTTP_PROXY=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
API 字段问题
gh repo view --json openIssuesCount
gh repo view --json issues --jq '.issues.totalCount'
gh repo view --json pullRequests --jq '.pullRequests.totalCount'
gh repo view --json issues,pullRequests,watchers,stargazerCount --jq '
"Issues: \(.issues.totalCount)",
"PRs: \(.pullRequests.totalCount)",
"Stars: \(.stargazerCount)",
"Watchers: \(.watchers.totalCount)"
'
常见错误码
| 错误 | 原因 | 解决方案 |
|---|
HTTP 403 | 权限不足/速率限制 | gh auth status 或等待后重试 |
HTTP 404 | 资源不存在/仓库名错误 | 检查 owner/repo 是否正确 |
unauthenticated | Token 过期 | gh auth login |
No default remote | 未设置默认仓库 | gh repo set-default 或 --repo 参数 |
Too many requests | API 速率限制 | 等待一分钟后重试 |
Unknown JSON field | 字段名错误或不存在 | 使用 --json name,description 查看可用字段 |
GitHub MCP vs gh CLI
| 维度 | gh CLI | GitHub MCP |
|---|
| 速度 | ~1.0s | ~0.8-1.2s(接近) |
| 输出格式 | JSON 字符串(需 jq 解析) | 结构化对象(直接可用) |
| 稳定性 | 中(API 变化需升级 gh) | 高(工具接口稳定) |
| 学习曲线 | 陡峭(记忆 --json/--jq 参数) | 平缓(LLM 友好) |
使用建议:
- 批量操作、脚本自动化 → gh CLI
- 单次查询、LLM 协同 → GitHub MCP