github-content

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

GitHub Content Fetching

GitHub 内容获取

When to activate:
  • User pastes a GitHub URL (issue, PR, repo, or source file)
  • User asks about PR comments, reviews, or discussions
  • User wants to browse or search code in a GitHub repository
  • User references a GitHub issue or PR by number and repo
Why use gh CLI instead of web fetching:
GitHub's web interface relies heavily on JavaScript to load comments, reviews, and discussions. Web fetching often retrieves only partial content. The
gh
CLI accesses the API directly and returns complete data.
激活场景:
  • 用户粘贴GitHub链接(issue、PR、仓库或源文件)
  • 用户询问PR评论、评审或讨论相关内容
  • 用户想要浏览或搜索GitHub仓库中的代码
  • 用户通过编号和仓库引用GitHub issue或PR
为什么选择gh CLI而非网页抓取:
GitHub的网页界面严重依赖JavaScript加载评论、评审和讨论内容。网页抓取通常只能获取部分内容,而
gh
CLI直接调用API,可返回完整数据。

Issues

Issues

View an issue with all comments:
bash
gh issue view <number> --repo owner/repo --comments
查看包含所有评论的issue:
bash
gh issue view <number> --repo owner/repo --comments

Pull Requests

Pull Requests

View a PR with comments and review status:
bash
gh pr view <number> --repo owner/repo --comments
View the diff for a PR:
bash
gh pr diff <number> --repo owner/repo
View PR review comments (inline code comments):
bash
gh api repos/owner/repo/pulls/<number>/comments
List files changed in a PR:
bash
gh pr view <number> --repo owner/repo --json files --jq '.files[].path'
查看包含评论和评审状态的PR:
bash
gh pr view <number> --repo owner/repo --comments
查看PR的差异对比:
bash
gh pr diff <number> --repo owner/repo
查看PR评审评论(行内代码评论):
bash
gh api repos/owner/repo/pulls/<number>/comments
列出PR中变更的文件:
bash
gh pr view <number> --repo owner/repo --json files --jq '.files[].path'

Repository Browsing

仓库浏览

List contents of a directory (defaults to root):
bash
gh api repos/owner/repo/contents/path/to/dir
View a specific file's contents:
bash
gh api repos/owner/repo/contents/path/to/file --jq '.content' | base64 -d
Get the full repository tree (all files):
bash
gh api repos/owner/repo/git/trees/main?recursive=1 --jq '.tree[] | select(.type=="blob") | .path'
Replace
main
with the appropriate branch name if needed.
列出目录内容(默认根目录):
bash
gh api repos/owner/repo/contents/path/to/dir
查看指定文件的内容:
bash
gh api repos/owner/repo/contents/path/to/file --jq '.content' | base64 -d
获取完整仓库树(所有文件):
bash
gh api repos/owner/repo/git/trees/main?recursive=1 --jq '.tree[] | select(.type=="blob") | .path'
如果需要,将
main
替换为对应分支名称。

Code Search

代码搜索

Search for code within a specific repository:
bash
gh search code "search query" --repo owner/repo
Search with language filter:
bash
gh search code "search query" --repo owner/repo --language python
Search with path filter:
bash
gh search code "search query" --repo owner/repo --path "src/"
View search results with more context:
bash
gh search code "search query" --repo owner/repo --json path,repository,textMatches
在指定仓库中搜索代码:
bash
gh search code "search query" --repo owner/repo
按语言过滤搜索:
bash
gh search code "search query" --repo owner/repo --language python
按路径过滤搜索:
bash
gh search code "search query" --repo owner/repo --path "src/"
查看包含更多上下文的搜索结果:
bash
gh search code "search query" --repo owner/repo --json path,repository,textMatches

Parsing GitHub URLs

解析GitHub链接

Extract owner/repo from common URL patterns:
URL PatternExample
github.com/owner/repo
github.com/zed-industries/zed
github.com/owner/repo/issues/123
Issue #123 in that repo
github.com/owner/repo/pull/456
PR #456 in that repo
github.com/owner/repo/blob/branch/path
Source file at path
github.com/owner/repo/tree/branch/path
Directory at path
从常见链接格式中提取owner/repo:
链接格式示例
github.com/owner/repo
github.com/zed-industries/zed
github.com/owner/repo/issues/123
该仓库中的Issue #123
github.com/owner/repo/pull/456
该仓库中的PR #456
github.com/owner/repo/blob/branch/path
指定路径下的源文件
github.com/owner/repo/tree/branch/path
指定路径下的目录

Tips

提示

  • For large outputs, pipe through
    head -n 100
    or use
    --jq
    to filter
  • Use
    --json
    flag with
    gh pr view
    or
    gh issue view
    for structured data
  • For private repos, ensure
    gh auth status
    shows you're authenticated
  • When browsing files, the API returns base64-encoded content that needs decoding
  • 对于大体积输出,可通过
    head -n 100
    进行管道处理,或使用
    --jq
    进行过滤
  • gh pr view
    gh issue view
    中使用
    --json
    标志获取结构化数据
  • 对于私有仓库,确保
    gh auth status
    显示已完成身份验证
  • 浏览文件时,API返回的是base64编码的内容,需要进行解码