github-cli

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

GitHub CLI Skill

GitHub CLI 技能

This skill provides comprehensive guidance for using the GitHub CLI (
gh
) to manage repositories, CI/CD workflows, issues, pull requests, and releases.
本技能提供了使用GitHub CLI(
gh
)管理仓库、CI/CD工作流、Issues、Pull Requests和Releases的全面指南。

Prerequisites

前提条件

  • gh
    CLI installed and authenticated
  • Run
    gh auth status
    to verify authentication
  • 已安装
    gh
    CLI并完成身份验证
  • 运行
    gh auth status
    以验证身份验证状态

Core Operations

核心操作

Repository Management

仓库管理

bash
undefined
bash
undefined

List repositories

List repositories

gh repo list [owner] --limit 50 gh repo list --source # Only source repos (not forks) gh repo list --fork # Only forks
gh repo list [owner] --limit 50 gh repo list --source # Only source repos (not forks) gh repo list --fork # Only forks

View repository

View repository

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

Create repository

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
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

Clone repository

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

Fork repository

Fork repository

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

Delete repository

Delete repository

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

Archive/Unarchive

Archive/Unarchive

gh repo archive <repo> gh repo unarchive <repo>
gh repo archive <repo> gh repo unarchive <repo>

Edit repository settings

Edit repository settings

gh repo edit --default-branch main gh repo edit --visibility public gh repo edit --enable-issues=false
undefined
gh repo edit --default-branch main gh repo edit --visibility public gh repo edit --enable-issues=false
undefined

Pull Requests

Pull Requests 操作

bash
undefined
bash
undefined

List PRs

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"
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

View PR

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

Create PR

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
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

Review PR

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

Merge PR

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
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

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
undefined
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
undefined

Issues

Issues 管理

bash
undefined
bash
undefined

List issues

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"
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

View issue

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

Create issue

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"
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

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"
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

Close/Reopen

gh issue close [number] gh issue close [number] --reason "not planned" gh issue reopen [number]
gh issue close [number] gh issue close [number] --reason "not planned" gh issue reopen [number]

Transfer issue

Transfer issue

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

Pin/Unpin issue

Pin/Unpin issue

gh issue pin [number] gh issue unpin [number]
undefined
gh issue pin [number] gh issue unpin [number]
undefined

GitHub Actions (Workflows)

GitHub Actions(工作流)

bash
undefined
bash
undefined

List workflows

List workflows

gh workflow list gh workflow list --all # Include disabled
gh workflow list gh workflow list --all # Include disabled

View workflow

View workflow

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

Run workflow manually

Run workflow manually

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

Enable/Disable workflow

Enable/Disable workflow

gh workflow enable [workflow] gh workflow disable [workflow]
gh workflow enable [workflow] gh workflow disable [workflow]

List workflow runs

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
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

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
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

Watch run in progress

gh run watch [run-id]
gh run watch [run-id]

Rerun workflow

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
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

Cancel run

gh run cancel [run-id]
gh run cancel [run-id]

Download artifacts

Download artifacts

gh run download [run-id] gh run download [run-id] -n artifact-name
undefined
gh run download [run-id] gh run download [run-id] -n artifact-name
undefined

Releases

Releases 管理

bash
undefined
bash
undefined

List releases

List releases

gh release list gh release list --exclude-drafts
gh release list gh release list --exclude-drafts

View release

View release

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

Create release

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
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

Edit release

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

Delete release

Delete release

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

Download assets

Download assets

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

Upload additional assets

Upload additional assets

gh release upload <tag> ./file.zip
undefined
gh release upload <tag> ./file.zip
undefined

Gists

Gists 操作

bash
undefined
bash
undefined

List gists

List gists

gh gist list gh gist list --public gh gist list --secret
gh gist list gh gist list --public gh gist list --secret

View gist

View gist

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

Create gist

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
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

Edit gist

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

Delete gist

Delete gist

gh gist delete [gist-id]
gh gist delete [gist-id]

Clone gist

Clone gist

gh gist clone [gist-id]
undefined
gh gist clone [gist-id]
undefined

GitHub API Direct Access

GitHub API 直接访问

bash
undefined
bash
undefined

GET request

GET request

gh api repos/{owner}/{repo} gh api /user gh api orgs/{org}/repos --paginate
gh api repos/{owner}/{repo} gh api /user gh api orgs/{org}/repos --paginate

POST request

POST request

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

With JSON

With JSON

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

GraphQL

GraphQL

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

Output formatting

Output formatting

gh api repos/{owner}/{repo} --jq '.name' gh api repos/{owner}/{repo} -t '{{.name}}'
undefined
gh api repos/{owner}/{repo} --jq '.name' gh api repos/{owner}/{repo} -t '{{.name}}'
undefined

Labels

标签管理

bash
undefined
bash
undefined

List labels

List labels

gh label list
gh label list

Create label

Create label

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

Edit label

Edit label

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

Delete label

Delete label

gh label delete "label-name"
gh label delete "label-name"

Clone labels from another repo

Clone labels from another repo

gh label clone source-repo
undefined
gh label clone source-repo
undefined

SSH Keys & GPG Keys

SSH 密钥与 GPG 密钥

bash
undefined
bash
undefined

List SSH keys

List SSH keys

gh ssh-key list
gh ssh-key list

Add SSH key

Add SSH key

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

Delete SSH key

Delete SSH key

gh ssh-key delete [key-id]
gh ssh-key delete [key-id]

List GPG keys

List GPG keys

gh gpg-key list
gh gpg-key list

Add GPG key

Add GPG key

gh gpg-key add key.gpg
undefined
gh gpg-key add key.gpg
undefined

Common Patterns

常见使用模式

Batch Operations

批量操作

bash
undefined
bash
undefined

Close all issues with specific label

Close all issues with specific label

gh issue list --label "wontfix" --json number --jq '.[].number' |
xargs -I {} gh issue close {}
gh issue list --label "wontfix" --json number --jq '.[].number' |
xargs -I {} gh issue close {}

Delete all draft releases

Delete all draft releases

gh release list --json tagName,isDraft --jq '.[] | select(.isDraft) | .tagName' |
xargs -I {} gh release delete {} --yes
gh release list --json tagName,isDraft --jq '.[] | select(.isDraft) | .tagName' |
xargs -I {} gh release delete {} --yes

Approve and merge all dependabot PRs

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'
undefined
gh pr list --author "app/dependabot" --json number --jq '.[].number' |
xargs -I {} sh -c 'gh pr review {} --approve && gh pr merge {} --squash'
undefined

JSON Output and Filtering

JSON 输出与过滤

bash
undefined
bash
undefined

Get specific fields

Get specific fields

gh pr list --json number,title,author gh issue list --json number,title,labels --jq '.[] | {num: .number, title: .title}'
gh pr list --json number,title,author gh issue list --json number,title,labels --jq '.[] | {num: .number, title: .title}'

Filter with jq

Filter with jq

gh pr list --json number,title,mergeable --jq '.[] | select(.mergeable == "MERGEABLE")'
undefined
gh pr list --json number,title,mergeable --jq '.[] | select(.mergeable == "MERGEABLE")'
undefined

Cross-Repository Operations

跨仓库操作

bash
undefined
bash
undefined

Specify repository explicitly

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
undefined
gh pr list -R owner/repo gh issue create -R owner/repo --title "Title" gh workflow run -R owner/repo workflow.yml
undefined

Troubleshooting

故障排除

bash
undefined
bash
undefined

Check authentication

Check authentication

gh auth status
gh auth status

Refresh authentication

Refresh authentication

gh auth refresh
gh auth refresh

Login with specific scopes

Login with specific scopes

gh auth login --scopes "repo,workflow,admin:org"
gh auth login --scopes "repo,workflow,admin:org"

Debug mode

Debug mode

GH_DEBUG=1 gh <command>
GH_DEBUG=1 gh <command>

Check rate limit

Check rate limit

gh api rate_limit
undefined
gh api rate_limit
undefined

Reference

参考资料

For detailed command reference including all flags and options, see
references/gh-commands.md
.
有关包含所有标志和选项的详细命令参考,请查看
references/gh-commands.md