mcpField
G
github-cli

github-cli

github.com/oldwinter开发10145 次安装2 周前更新
代码仓库

关于 github-cli

指导使用 GitHub CLI(`gh`)管理仓库、拉取请求、议题、CI/CD 工作流、发布等 GitHub 资源,适合需要命令参考的用户。内容还包括 JSON 筛选、API 访问和常见批量操作。

主要功能

  1. 01仓库与项目管理
  2. 02拉取请求和议题操作
  3. 03GitHub Actions 工作流管理
  4. 04发布与 gist 命令
  5. 05API 请求与 JSON 筛选
  6. 06批量操作与故障排查

适用场景

  1. 01查看并筛选拉取请求
  2. 02跨仓库管理议题
  3. 03检查或运行 GitHub Actions
  4. 04创建发布或查询 GitHub API

安装

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

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

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

附带文件references/gh-commands.md

详细介绍

GitHub CLI 技能

此技能全面介绍如何使用 GitHub CLI(gh)管理仓库、CI/CD 工作流、议题、拉取请求和发布。

前提条件

  • 已安装并完成身份验证的 gh CLI
  • 运行 gh auth status 以验证身份验证状态

核心操作

仓库管理
# List repositories
gh repo list [owner] --limit 50
gh repo list --source                    # Only source repos (not forks)
gh repo list --fork                      # Only forks

# View repository
gh repo view [repo]
gh repo view --web                       # Open in browser

# Create repository
gh repo create <name> --public           # Public repo
gh repo create <name> --private          # Private repo
gh repo create <name> --clone            # Create and clone locally
gh repo create <name> --template <repo>  # From template

# Clone repository
gh repo clone <repo>
gh repo clone <repo> -- --depth 1        # Shallow clone

# Fork repository
gh repo fork <repo>
gh repo fork <repo> --clone              # Fork and clone

# Delete repository
gh repo delete <repo> --yes              # Requires confirmation

# Archive/Unarchive
gh repo archive <repo>
gh repo unarchive <repo>

# Edit repository settings
gh repo edit --default-branch main
gh repo edit --visibility public
gh repo edit --enable-issues=false
拉取请求
# List PRs
gh pr list
gh pr list --state all                   # All states
gh pr list --author @me                  # My PRs
gh pr list --search "is:open draft:false"

# View PR
gh pr view [number]
gh pr view --web                         # Open in browser
gh pr view --comments                    # Show comments

# Create PR
gh pr create                             # Interactive
gh pr create --title "Title" --body "Description"
gh pr create --draft                     # Create as draft
gh pr create --base main --head feature  # Specify branches
gh pr create --fill                      # Auto-fill from commits

# Review PR
gh pr review [number] --approve
gh pr review [number] --request-changes --body "Comments"
gh pr review [number] --comment --body "LGTM"

# Merge PR
gh pr merge [number]
gh pr merge --merge                      # Merge commit
gh pr merge --squash                     # Squash and merge
gh pr merge --rebase                     # Rebase and merge
gh pr merge --auto                       # Enable auto-merge
gh pr merge --delete-branch              # Delete branch after merge

# Other PR operations
gh pr checkout [number]                  # Checkout PR locally
gh pr ready [number]                     # Mark as ready for review
gh pr close [number]
gh pr reopen [number]
gh pr diff [number]
gh pr checks [number]                    # View CI status
议题
# List issues
gh issue list
gh issue list --state all
gh issue list --label "bug"
gh issue list --assignee @me
gh issue list --search "is:open label:urgent"

# View issue
gh issue view [number]
gh issue view --web
gh issue view --comments

# Create issue
gh issue create                          # Interactive
gh issue create --title "Title" --body "Description"
gh issue create --label "bug,urgent"
gh issue create --assignee "@me,user2"
gh issue create --milestone "v1.0"

# Edit issue
gh issue edit [number] --title "New title"
gh issue edit [number] --add-label "priority"
gh issue edit [number] --remove-label "wontfix"
gh issue edit [number] --add-assignee "user"

# Close/Reopen
gh issue close [number]
gh issue close [number] --reason "not planned"
gh issue reopen [number]

# Transfer issue
gh issue transfer [number] <destination-repo>

# Pin/Unpin issue
gh issue pin [number]
gh issue unpin [number]
GitHub Actions(工作流)
# List workflows
gh workflow list
gh workflow list --all                   # Include disabled

# View workflow
gh workflow view [workflow-id|name]
gh workflow view --web

# Run workflow manually
gh workflow run [workflow]
gh workflow run [workflow] --ref branch-name
gh workflow run [workflow] -f param1=value1 -f param2=value2

# Enable/Disable workflow
gh workflow enable [workflow]
gh workflow disable [workflow]

# List workflow runs
gh run list
gh run list --workflow [workflow]
gh run list --branch main
gh run list --status failure
gh run list --user @me

# View run details
gh run view [run-id]
gh run view --web
gh run view --log                        # Full logs
gh run view --log-failed                 # Only failed job logs

# Watch run in progress
gh run watch [run-id]

# Rerun workflow
gh run rerun [run-id]
gh run rerun [run-id] --failed           # Only failed jobs
gh run rerun [run-id] --debug            # With debug logging

# Cancel run
gh run cancel [run-id]

# Download artifacts
gh run download [run-id]
gh run download [run-id] -n artifact-name
发布
# List releases
gh release list
gh release list --exclude-drafts

# View release
gh release view [tag]
gh release view --web

# Create release
gh release create <tag>                  # Interactive
gh release create <tag> --title "Title" --notes "Notes"
gh release create <tag> --generate-notes # Auto-generate notes
gh release create <tag> --draft          # Create as draft
gh release create <tag> --prerelease     # Mark as pre-release
gh release create <tag> ./dist/*         # Upload assets

# Edit release
gh release edit <tag> --title "New title"
gh release edit <tag> --draft=false      # Publish draft

# Delete release
gh release delete <tag>
gh release delete <tag> --cleanup-tag    # Also delete tag

# Download assets
gh release download <tag>
gh release download <tag> -p "*.zip"     # Pattern match

# Upload additional assets
gh release upload <tag> ./file.zip
Gist
# List gists
gh gist list
gh gist list --public
gh gist list --secret

# View gist
gh gist view [gist-id]
gh gist view --web

# Create gist
gh gist create file.txt                  # Single file
gh gist create file1.txt file2.txt       # Multiple files
gh gist create --public file.txt         # Public gist
gh gist create -d "Description" file.txt

# Edit gist
gh gist edit [gist-id]
gh gist edit [gist-id] -a newfile.txt    # Add file

# Delete gist
gh gist delete [gist-id]

# Clone gist
gh gist clone [gist-id]
直接访问 GitHub API
# GET request
gh api repos/{owner}/{repo}
gh api /user
gh api orgs/{org}/repos --paginate

# POST request
gh api repos/{owner}/{repo}/issues -f title="Bug" -f body="Description"

# With JSON
gh api repos/{owner}/{repo}/labels --input data.json

# GraphQL
gh api graphql -f query='{ viewer { login } }'

# Output formatting
gh api repos/{owner}/{repo} --jq '.name'
gh api repos/{owner}/{repo} -t '{{.name}}'
标签
# List labels
gh label list

# Create label
gh label create "priority:high" --color FF0000 --description "High priority"

# Edit label
gh label edit "old-name" --name "new-name"
gh label edit "bug" --color 00FF00

# Delete label
gh label delete "label-name"

# Clone labels from another repo
gh label clone source-repo
SSH 密钥和 GPG 密钥
# List SSH keys
gh ssh-key list

# Add SSH key
gh ssh-key add ~/.ssh/id_rsa.pub --title "My Key"

# Delete SSH key
gh ssh-key delete [key-id]

# List GPG keys
gh gpg-key list

# Add GPG key
gh gpg-key add key.gpg

常见模式

批量操作
# Close all issues with specific label
gh issue list --label "wontfix" --json number --jq '.[].number' | \
  xargs -I {} gh issue close {}

# Delete all draft releases
gh release list --json tagName,isDraft --jq '.[] | select(.isDraft) | .tagName' | \
  xargs -I {} gh release delete {} --yes

# Approve and merge all dependabot PRs
gh pr list --author "app/dependabot" --json number --jq '.[].number' | \
  xargs -I {} sh -c 'gh pr review {} --approve && gh pr merge {} --squash'
JSON 输出和筛选
# Get specific fields
gh pr list --json number,title,author
gh issue list --json number,title,labels --jq '.[] | {num: .number, title: .title}'

# Filter with jq
gh pr list --json number,title,mergeable --jq '.[] | select(.mergeable == "MERGEABLE")'
跨仓库操作
# Specify repository explicitly
gh pr list -R owner/repo
gh issue create -R owner/repo --title "Title"
gh workflow run -R owner/repo workflow.yml

故障排除

# Check authentication
gh auth status

# Refresh authentication
gh auth refresh

# Login with specific scopes
gh auth login --scopes "repo,workflow,admin:org"

# Debug mode
GH_DEBUG=1 gh <command>

# Check rate limit
gh api rate_limit

参考

如需包含所有标志和选项的详细命令参考,请参阅 references/gh-commands.md。

评论

支持 Markdown