mcpField
G
gh-cli

gh-cli

github.com/logicrw开发02.7k 次安装33 周前更新
代码仓库

关于 gh-cli

面向 Agent 的 GitHub 命令行工具 `gh` 使用技能,涵盖仓库相关操作,并通过筛选 JSON 字段和限制输出减少 Agent 接收的信息。

主要功能

  1. 01处理 GitHub PR 和议题
  2. 02访问仓库与文件
  3. 03搜索代码和仓库
  4. 04查询提交、发布版本及 API
  5. 05筛选 JSON 输出的指导

适用场景

  1. 01查看 PR 详情和差异
  2. 02创建或检查 GitHub 议题
  3. 03查找代码或仓库
  4. 04获取精简的提交或发布版本信息

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/logicrw/gh-cli-skill --skill gh-cli

安装后在 Claude Code 中输入/gh-cli确认已加载。

详细介绍

GitHub CLI(gh)——Agent 技能

所有 GitHub 操作都使用 gh CLI。始终使用 --json 获取结构化输出,并使用 --jq 选择字段,以尽量减少令牌用量。

身份验证

已通过 gh auth 配置。使用以下命令验证:

gh auth status

核心原则

始终只请求所需字段,以尽量减少输出令牌:

# ❌ Bad — returns everything
gh pr view 123

# ✅ Good — returns only what's needed
gh pr view 123 --json title,state,body,url --jq '{title,state,body,url}'

快速参考

拉取请求
# List open PRs
gh pr list --json number,title,author,updatedAt --jq '.[] | {number,title,author:.author.login}'

# View PR details
gh pr view <number> --json title,state,body,headRefName,baseRefName,additions,deletions,changedFiles

# View PR diff
gh pr diff <number>

# View PR files changed
gh pr diff <number> --name-only

# Create PR
gh pr create --title "title" --body "description" --base main --head feature-branch

# Create draft PR
gh pr create --title "title" --body "description" --draft

# Merge PR
gh pr merge <number> --squash --delete-branch

# Review PR
gh pr review <number> --approve
gh pr review <number> --request-changes --body "feedback"
gh pr review <number> --comment --body "looks good"

# PR review comments
gh api repos/{owner}/{repo}/pulls/{number}/comments --jq '.[].body'

# Add review comment on specific line
gh api repos/{owner}/{repo}/pulls/{number}/comments \
  -f body="comment" -f path="file.py" -F line=42 -f side="RIGHT" \
  -f commit_id="$(gh pr view <number> --json headRefOid --jq .headRefOid)"

# Check PR CI status
gh pr checks <number>

# Update PR branch
gh pr update-branch <number>
问题
# List issues
gh issue list --json number,title,labels,assignees --jq '.[] | {number,title,labels:[.labels[].name]}'

# View issue
gh issue view <number> --json title,body,state,labels,assignees,comments

# Create issue
gh issue create --title "title" --body "description" --label "bug"

# Comment on issue
gh issue comment <number> --body "comment text"

# Close issue
gh issue close <number> --reason completed

# Edit issue
gh issue edit <number> --title "new title" --add-label "priority"

# Search issues
gh issue list --search "keyword in:title,body" --json number,title
仓库与文件
# View repo info
gh repo view --json name,description,defaultBranchRef --jq '{name,description,branch:.defaultBranchRef.name}'

# Get file contents (use gh api to avoid raw output issues)
gh api repos/{owner}/{repo}/contents/{path} --jq '.content' | base64 -d
# Or with ref:
gh api repos/{owner}/{repo}/contents/{path}?ref={branch} --jq '.content' | base64 -d

# List directory
gh api repos/{owner}/{repo}/contents/{path} --jq '.[].name'

# Create/update file
gh api repos/{owner}/{repo}/contents/{path} \
  -X PUT \
  -f message="commit message" \
  -f content="$(echo -n 'file content' | base64)" \
  -f branch="branch-name"
# For update, include sha:
  -f sha="<blob_sha>"

# Delete file
gh api repos/{owner}/{repo}/contents/{path} \
  -X DELETE \
  -f message="delete file" \
  -f sha="<blob_sha>" \
  -f branch="branch-name"

# Push multiple files — use git directly
git add file1 file2 && git commit -m "message" && git push

# Create branch
gh api repos/{owner}/{repo}/git/refs \
  -f ref="refs/heads/new-branch" \
  -f sha="$(gh api repos/{owner}/{repo}/git/ref/heads/main --jq '.object.sha')"

# List branches
gh api repos/{owner}/{repo}/branches --jq '.[].name'

# Fork repo
gh repo fork {owner}/{repo}

# Create repo
gh repo create {name} --public --description "desc"
搜索
# Search code
gh search code "query" --repo {owner}/{repo} --json path,repository,textMatches
gh search code "query language:python" --json path,repository

# Search repos
gh search repos "query" --json fullName,description,stargazersCount --jq '.[] | {name:.fullName,stars:.stargazersCount}'

# Search issues across GitHub
gh search issues "query" --json number,title,repository

# Search PRs
gh search prs "query" --json number,title,repository
提交与历史记录
# List commits
gh api repos/{owner}/{repo}/commits --jq '.[].commit.message' | head -20
gh api repos/{owner}/{repo}/commits?sha={branch}\&per_page=10 --jq '.[] | {sha: .sha[:7], message: .commit.message}'

# View specific commit
gh api repos/{owner}/{repo}/commits/{sha} --jq '{message:.commit.message,files:[.files[].filename],stats:.stats}'

# Get commit diff
gh api repos/{owner}/{repo}/commits/{sha} -H "Accept: application/vnd.github.diff"
发布与标签
# List releases
gh release list --json tagName,name,publishedAt

# View latest release
gh release view --json tagName,name,body

# View specific release
gh release view {tag} --json tagName,name,body

# List tags
gh api repos/{owner}/{repo}/tags --jq '.[].name'

# Create release
gh release create {tag} --title "title" --notes "release notes"
高级 API 访问

对于上述未涵盖的操作,直接使用 gh api:

# GET request
gh api repos/{owner}/{repo}/actions/runs --jq '.workflow_runs[:3] | .[].conclusion'

# POST request
gh api repos/{owner}/{repo}/issues/{number}/comments -f body="comment"

# With pagination
gh api repos/{owner}/{repo}/issues --paginate --jq '.[].number'

# Get authenticated user
gh api user --jq '{login,name,email}'

# Team members
gh api orgs/{org}/teams/{team}/members --jq '.[].login'

输出优化

始终尽量减少 Agent 消费的输出:

  1. 使用 --json + --jq 获取结构化、精简的输出
  2. 使用 --limit 或 API 的 per_page 参数限制结果数量
  3. 选择字段——切勿输出完整对象
  4. 对于较长的输出,管道传给 head:| head -50

错误处理

# Check if command succeeded
gh pr view 999 --json state 2>/dev/null && echo "exists" || echo "not found"

# Safe API call with error handling
gh api repos/{owner}/{repo}/contents/{path} 2>/dev/null || echo "file not found"

评论

支持 Markdown