Github Cli — Github 命令行工具
v2Comprehensive GitHub 命令行工具 (gh) reference. Covers repos, issues, PRs, Actions, releases, gists, 搜索, projects v2, API, secrets/variables, labels, codespaces, 扩展s, auth, and advanced GraphQL patterns.
运行时依赖
安装命令
点击复制技能文档
GitHub 命令行工具 (gh) — Comprehensive 技能
Version: gh 2.66.1+ Auth: gh auth 记录in or 设置 GH_令牌 env var Always use --repo OWNER/REPO (or -R) when not inside a git repo directory.
Table of Contents Authentication & Config Repositories Issues Pull 请求s GitHub Actions (运行s & 工作流s) Releases Gists 搜索 Labels Secrets & Variables 缓存s Projects V2 API (REST & GraphQL) 扩展s Codespaces Copilot Other Commands JSON 输出 & 格式化ting 环境 Variables Advanced Patterns Tips & Gotchas
- Authentication & Config
# 记录in with a PAT from stdin echo "$MY_令牌" | gh auth 记录in --with-令牌
# 记录in to GitHub Enterprise gh auth 记录in --hostname enterprise.example.com
# 添加 extra scopes (e.g., project scope for Projects V2) gh auth refresh -s project
# 添加 删除_repo scope gh auth refresh -s 删除_repo
# 检查 auth 状态 (shows active account, scopes, 令牌 validity) gh auth 状态 gh auth 状态 --show-令牌
# Switch active account (when multiple accounts 配置d) gh auth switch
# Print the active 令牌 (useful for piping to other 工具s) gh auth 令牌
# 记录out gh auth 记录out
Required scopes by feature:
Feature Scope needed Basic repo/PR/issue ops repo Gists gist Read org membership read:org Projects V2 project 删除 repos 删除_repo Actions 工作流s 工作流 Read user 性能分析 user Config # 列出 all config gh config 列出
# 获取/设置 individual values gh config 获取 git_protocol # https or ssh gh config 设置 git_protocol ssh gh config 设置 editor "code --wAIt" gh config 设置 pager "less -R" gh config 设置 prompt disabled # disable interactive prompts (good for scripts) gh config 设置 browser "firefox"
# Clear 命令行工具 缓存 gh config clear-缓存
Git 凭证 设置up # 配置 git to use gh for HTTPS auth gh auth 设置up-git
- Repositories
# Public repo, clone locally gh repo 创建 my-project --public --clone
# In an org gh repo 创建 my-org/my-project --private
# From local directory gh repo 创建 my-project --private --source=. --remote=up流 --push
# From template gh repo 创建 my-project --template owner/template-repo --clone
# With options gh repo 创建 my-project --public --description "My project" \ --license mit --gitignore Node --添加-readme
Clone gh repo clone owner/repo gh repo clone owner/repo my-dir gh repo clone owner/repo -- --depth=1 # shallow clone
# Clone your own repo (owner defaults to you) gh repo clone my-repo
Fork # Fork current repo gh repo fork
# Fork and clone gh repo fork owner/repo --clone
# Fork into an org gh repo fork owner/repo --org my-org --fork-name new-name
# Fork default branch only gh repo fork owner/repo --default-branch-only
View # View current repo (README + description) gh repo view gh repo view owner/repo
# Open in browser gh repo view --网页
# JSON 输出 gh repo view --json name,description,stargazerCount,url gh repo view --json name,stargazerCount --jq '.stargazerCount'
JSON fields for repo: 归档dAt, as签名ableUsers, codeOfConduct, 创建dAt, defaultBranchRef, 删除BranchOnMerge, description, diskUsage, forkCount, hasDiscussionsEnabled, hasIssuesEnabled, hasProjectsEnabled, hasWikiEnabled, homepageUrl, id, is归档d, isEmpty, isFork, isPrivate, isTemplate, languages, latestRelease, license信息, name, nameWithOwner, owner, parent, primaryLanguage, pull请求s, pushedAt, sshUrl, stargazerCount, 更新dAt, url, visibility, watchers
列出 # Your repos gh repo 列出 gh repo 列出 --limit 100
# Another user/org's repos gh repo 列出 my-org
# 过滤器 gh repo 列出 --language go --visibility public gh repo 列出 --topic 命令行工具 --no-归档d gh repo 列出 --fork # only forks gh repo 列出 --source # only non-forks
# JSON 输出 gh repo 列出 --json name,stargazerCount --jq '.[] | "\(.name): \(.stargazerCount) stars"'
Edit # Edit 设置tings gh repo edit --description "New description" gh repo edit --homepage "https://example.com" gh repo edit --enable-issues --enable-wiki gh repo edit --enable-projects=false gh repo edit --default-branch mAIn gh repo edit --enable-auto-merge gh repo edit --删除-branch-on-merge gh repo edit --添加-topic "命令行工具,自动化" gh repo edit --移除-topic "old-topic" gh repo edit --template # make it a template repo
# Change visibility (DANGEROUS — requires acknowledgment) gh repo edit --visibility public --accept-visibility-change-consequences
删除 / 归档 gh repo 删除 owner/repo --yes # requires 删除_repo scope gh repo 归档 owner/repo --yes gh repo un归档 owner/repo --yes
Rename gh repo rename new-name # renames current repo gh repo rename new-name -R owner/repo
设置 Default # 设置 which remote is used for gh commands in this local clone gh repo 设置-default owner/repo gh repo 设置-default --view # see current default gh repo 设置-default --un设置
同步 (Fork ↔ Up流) # 同步 local repo from remote parent gh repo 同步
# 同步 specific branch