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 statusCore 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,titleRepository & 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,repositoryCommits & 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:
- Use
--json+--jqfor structured, minimal output - Limit results with
--limitor APIper_pageparameter - Select fields — never dump full objects
- Pipe to
headfor 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"