Gh Cli

logicrw/gh-cli-skill/gh-cli

作者 logicrwd8226adbaca09f5b282d20dd91c6ead62f98305c無授權條款收錄於 2026年10月9日更新於 2026年10月9日

GitHub CLI operations via `gh` command. Use when working with GitHub repositories, pull requests, issues, code search, releases, or any GitHub API operation. This skill replaces the GitHub MCP server with direct CLI calls. Triggers on: GitHub, PR, pull request, issue, repo, branch, release, tag, commit, fork, merge, review, gh.

AI 產生的概覽

指導代理使用 GitHub CLI(gh)進行儲存庫、提取要求、議題、搜尋、發行版本及 API 操作。

功能
此技能是一份僅含說明的參考文件,用於透過 gh 命令列工具執行 GitHub 操作。內容涵蓋身分驗證檢查,提取要求的列出、檢視、建立、合併與審查,議題管理,儲存庫與檔案操作,程式碼與儲存庫搜尋,提交歷史,發行版本與標籤,以及直接呼叫 gh api。它強調使用 --json 與 --jq 輸出結構化結果、限制回傳數量並進行基本錯誤處理,以控制代理的輸出量。
適用情境
當任務涉及 GitHub 儲存庫、提取要求、議題、程式碼搜尋、發行版本、分支、提交或任何 GitHub API 操作時使用。它旨在透過直接呼叫 CLI 取代 GitHub MCP 伺服器。
執行需求
需要安裝 gh CLI 並透過 gh auth 完成身分驗證,且可存取 GitHub 網路。不含指令碼,僅為說明文件。

GitHub CLI (gh) — Agent Skill

Use the gh CLI for ALL GitHub operations. Always use --json for structured output and --jq for field selection to minimize token usage.

Authentication

Already configured via gh auth. Verify with:

bash
gh auth status

Core Principle

Always request only the fields you need to minimize output tokens:

bash
# ❌ Bad — returns everythinggh pr view 123
# ✅ Good — returns only what's neededgh pr view 123 --json title,state,body,url --jq '{title,state,body,url}'

Quick Reference

Pull Requests

bash
# List open PRsgh pr list --json number,title,author,updatedAt --jq '.[] | {number,title,author:.author.login}'
# View PR detailsgh pr view <number> --json title,state,body,headRefName,baseRefName,additions,deletions,changedFiles
# View PR diffgh pr diff <number>
# View PR files changedgh pr diff <number> --name-only
# Create PRgh pr create --title "title" --body "description" --base main --head feature-branch
# Create draft PRgh pr create --title "title" --body "description" --draft
# Merge PRgh pr merge <number> --squash --delete-branch
# Review PRgh pr review <number> --approvegh pr review <number> --request-changes --body "feedback"gh pr review <number> --comment --body "looks good"
# PR review commentsgh api repos/{owner}/{repo}/pulls/{number}/comments --jq '.[].body'
# Add review comment on specific linegh 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 statusgh pr checks <number>
# Update PR branchgh pr update-branch <number>

Issues

bash
# List issuesgh issue list --json number,title,labels,assignees --jq '.[] | {number,title,labels:[.labels[].name]}'
# View issuegh issue view <number> --json title,body,state,labels,assignees,comments
# Create issuegh issue create --title "title" --body "description" --label "bug"
# Comment on issuegh issue comment <number> --body "comment text"
# Close issuegh issue close <number> --reason completed
# Edit issuegh issue edit <number> --title "new title" --add-label "priority"
# Search issuesgh issue list --search "keyword in:title,body" --json number,title

Repository & Files

bash
# View repo infogh 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 directorygh api repos/{owner}/{repo}/contents/{path} --jq '.[].name'
# Create/update filegh 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 filegh 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 directlygit add file1 file2 && git commit -m "message" && git push
# Create branchgh 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 branchesgh api repos/{owner}/{repo}/branches --jq '.[].name'
# Fork repogh repo fork {owner}/{repo}
# Create repogh repo create {name} --public --description "desc"

Search

bash
# Search codegh search code "query" --repo {owner}/{repo} --json path,repository,textMatchesgh search code "query language:python" --json path,repository
# Search reposgh search repos "query" --json fullName,description,stargazersCount --jq '.[] | {name:.fullName,stars:.stargazersCount}'
# Search issues across GitHubgh search issues "query" --json number,title,repository
# Search PRsgh search prs "query" --json number,title,repository

Commits & History

bash
# List commitsgh api repos/{owner}/{repo}/commits --jq '.[].commit.message' | head -20gh api repos/{owner}/{repo}/commits?sha={branch}\&per_page=10 --jq '.[] | {sha: .sha[:7], message: .commit.message}'
# View specific commitgh api repos/{owner}/{repo}/commits/{sha} --jq '{message:.commit.message,files:[.files[].filename],stats:.stats}'
# Get commit diffgh api repos/{owner}/{repo}/commits/{sha} -H "Accept: application/vnd.github.diff"

Releases & Tags

bash
# List releasesgh release list --json tagName,name,publishedAt
# View latest releasegh release view --json tagName,name,body
# View specific releasegh release view {tag} --json tagName,name,body
# List tagsgh api repos/{owner}/{repo}/tags --jq '.[].name'
# Create releasegh release create {tag} --title "title" --notes "release notes"

Advanced API Access

For any operation not covered above, use gh api directly:

bash
# GET requestgh api repos/{owner}/{repo}/actions/runs --jq '.workflow_runs[:3] | .[].conclusion'
# POST requestgh api repos/{owner}/{repo}/issues/{number}/comments -f body="comment"
# With paginationgh api repos/{owner}/{repo}/issues --paginate --jq '.[].number'
# Get authenticated usergh api user --jq '{login,name,email}'
# Team membersgh api orgs/{org}/teams/{team}/members --jq '.[].login'

Output Optimization

Always minimize output for agent consumption:

  1. Use --json + --jq for structured, minimal output
  2. Limit results with --limit or API per_page parameter
  3. Select fields — never dump full objects
  4. Pipe to head for long outputs: | head -50

Error Handling

bash
# Check if command succeededgh pr view 999 --json state 2>/dev/null && echo "exists" || echo "not found"
# Safe API call with error handlinggh api repos/{owner}/{repo}/contents/{path} 2>/dev/null || echo "file not found"

來源與署名

來源:logicrw/gh-cli-skill位於gh-cli提交d8226ad

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架