AgentSkillsCN

gh-cli

GitHub CLI(gh)是面向仓库、议题、拉取请求、Actions、项目、发布、Gist、Codespaces、组织,以及所有 GitHub 操作的全面命令行参考工具。适用于创建 PR、管理议题、运行工作流、检查 CI 状态、管理发布,或进行 GitHub API 调用的场景。触发条件包括:“创建 PR”、“列出议题”、“gh 命令”、“合并 PR”、“运行工作流”、“检查状态”、“创建发布”、“下载制品”、“设置密钥”。

SKILL.md
--- frontmatter
name: gh-cli
description: GitHub CLI (gh) comprehensive reference for repositories, issues, pull requests, Actions, projects, releases, gists, codespaces, organizations, extensions, and all GitHub operations from the command line. Use when creating PRs, managing issues, running workflows, checking CI status, managing releases, or making GitHub API calls. Triggers on "create PR", "list issues", "gh command", "merge PR", "run workflow", "check status", "create release", "download artifacts", "set secret".
source: https://github.com/github/awesome-copilot/blob/main/skills/gh-cli/SKILL.md

GitHub CLI (gh)

Work seamlessly with GitHub from the command line.

Quick Reference

TaskCommand
Create PRgh pr create --title "..." --body "..."
List open PRsgh pr list
View PRgh pr view 123
Merge PRgh pr merge 123 --squash --delete-branch
Checkout PRgh pr checkout 123
Create issuegh issue create --title "..." --body "..."
List issuesgh issue list
Close issuegh issue close 123 --comment "Fixed in #456"
Clone repogh repo clone owner/repo
Create repogh repo create my-repo --public
List workflow runsgh run list
Watch workflowgh run watch 123456789
Run workflowgh workflow run ci.yml
Download artifactsgh run download 123456789
Create releasegh release create v1.0.0 --notes "..."
Set secretgh secret set MY_SECRET
API requestgh api /user

CLI Structure

code
gh                          # Root command
├── auth                    # Authentication
├── browse                  # Open in browser
├── repo                    # Repositories
├── issue                   # Issues
├── pr                      # Pull Requests
├── run                     # Workflow runs
├── workflow                # Workflows
├── release                 # Releases
├── project                 # Projects
├── codespace               # Codespaces
├── gist                    # Gists
├── cache                   # Actions caches
├── secret                  # Secrets
├── variable                # Variables
├── api                     # API requests
├── search                  # Search
├── label                   # Labels
├── org                     # Organizations
├── ssh-key                 # SSH keys
├── gpg-key                 # GPG keys
├── extension               # Extensions
├── alias                   # Aliases
├── config                  # Configuration
├── ruleset                 # Rulesets
├── attestation             # Attestations
├── status                  # Status overview
└── completion              # Shell completion

Detailed References

For comprehensive command documentation:

  • Authentication - Login, tokens, credentials, environment variables
  • Repositories - Create, clone, fork, sync, browse, edit
  • Issues - Create, list, edit, close, comment, labels
  • Pull Requests - Create, review, merge, checkout, diff
  • Actions - Workflows, runs, caches, secrets, variables
  • Releases - Create, upload, download, verify
  • Projects - Create, manage items, fields
  • Codespaces - Create, connect, manage
  • API - REST and GraphQL requests
  • Misc - Gists, orgs, search, labels, keys, extensions, aliases

Global Flags

FlagDescription
--help / -hShow help for command
--repo [HOST/]OWNER/REPOSelect another repository
--hostname HOSTGitHub hostname
--jq EXPRESSIONFilter JSON output
--json FIELDSOutput JSON with specified fields
--template STRINGFormat JSON using Go template
--webOpen in browser
--paginateMake additional API calls

Output Formatting

JSON Output

bash
# Basic JSON
gh repo view --json name,description

# Nested fields
gh repo view --json owner,name --jq '.owner.login + "/" + .name'

# Array operations
gh pr list --json number,title --jq '.[] | select(.number > 100)'

# Complex queries
gh issue list --json number,title,labels \
  --jq '.[] | {number, title: .title, tags: [.labels[].name]}'

Template Output

bash
# Custom template
gh repo view --template '{{.name}}: {{.description}}'

# Multiline template
gh pr view 123 --template 'Title: {{.title}}
Author: {{.author.login}}
State: {{.state}}'

Common Workflows

Create PR from Issue

bash
# Create branch from issue
gh issue develop 123 --branch feature/issue-123

# Make changes, commit, push
git add . && git commit -m "Fix issue #123" && git push

# Create PR linking to issue
gh pr create --title "Fix #123" --body "Closes #123"

Bulk Operations

bash
# Close multiple issues
gh issue list --search "label:stale" --json number --jq '.[].number' | \
  xargs -I {} gh issue close {} --comment "Closing as stale"

# Add label to multiple PRs
gh pr list --search "review:required" --json number --jq '.[].number' | \
  xargs -I {} gh pr edit {} --add-label needs-review

Repository Setup

bash
# Create repository with initial setup
gh repo create my-project --public \
  --description "My awesome project" \
  --clone --gitignore python --license mit

cd my-project

# Create labels
gh label create bug --color "d73a4a" --description "Bug report"
gh label create enhancement --color "a2eeef" --description "Feature request"

CI/CD Workflow

bash
# Run workflow
gh workflow run ci.yml --ref main

# Get latest run
RUN_ID=$(gh run list --workflow ci.yml --limit 1 --json databaseId --jq '.[0].databaseId')

# Watch the run
gh run watch "$RUN_ID"

# Download artifacts on completion
gh run download "$RUN_ID" --dir ./artifacts

Fork and Sync

bash
# Fork repository
gh repo fork original/repo --clone
cd repo

# Sync fork with upstream
gh repo sync

Environment Variables

bash
export GH_TOKEN=ghp_xxxxxxxxxxxx    # Token for automation
export GH_HOST=github.com           # GitHub hostname
export GH_PROMPT_DISABLED=true      # Disable prompts
export GH_REPO=owner/repo           # Override default repo

Best Practices

  1. Set default repository to avoid repetition:

    bash
    gh repo set-default owner/repo
    
  2. Use JSON + jq for complex data extraction:

    bash
    gh pr list --json number,title --jq '.[] | select(.title | contains("fix"))'
    
  3. Use --paginate for large result sets:

    bash
    gh issue list --state all --paginate
    
  4. Use environment variables for automation:

    bash
    export GH_TOKEN=$(gh auth token)
    

Getting Help

bash
gh --help              # General help
gh pr --help           # Command help
gh issue create --help # Subcommand help
gh help formatting     # Help topics
gh help environment

References