Appearance
GitHub CLI and Repository Patterns
Core patterns for working with GitHub repositories using the gh CLI and Git.
When to use
Use the gh CLI for common repository operations including setup, authentication, cloning, forking, syncing, querying metadata, and managing releases.
The pattern
Authentication
Authenticate once with the GitHub CLI:
sh
gh auth loginVerify authentication status:
sh
gh auth statusRepository setup
Clone
sh
gh repo clone owner/repoCreate
sh
gh repo create my-repo --public --cloneFork and clone
sh
gh repo fork owner/repo --cloneCommon operations
View repository info
sh
gh repo view owner/repoSync fork with upstream
sh
gh repo sync owner/repoList repositories
sh
gh repo list owner --limit 50Set default repository
When working outside a Git repo or when the remote is ambiguous:
sh
gh repo set-default owner/repoReleases
Create a release
sh
gh release create v1.0.0 --title "v1.0.0" --notes "Release notes"Create a release from a tag
sh
gh release create v1.0.0 --generate-notesUpload assets to a release
sh
gh release upload v1.0.0 ./dist/artifact.zipRepository settings via CLI
Set topics
sh
gh repo edit --add-topic "javascript,web-components"Configure default branch
sh
gh repo edit --default-branch mainAPI access
Use gh api for operations not covered by built-in commands:
sh
# Get repository details
gh api repos/owner/repo
# Use GraphQL
gh api graphql -f query='
query {
repository(owner: "owner", name: "repo") {
description
stargazerCount
}
}
'Pagination
Use --paginate for endpoints that return lists:
sh
gh api repos/owner/repo/issues --paginateJSON filtering
Use --jq to extract specific fields:
sh
gh api repos/owner/repo --jq '.default_branch'Trade-offs
ghCLI vs rawgit: Useghfor GitHub-specific operations (issues, PRs, releases, API). Usegitfor standard version control (commit, branch, merge).gh apivs built-in commands: Prefer built-in commands when available (gh issue listovergh api repos/.../issues). Fall back togh apifor advanced queries or missing features.- GraphQL vs REST: Use GraphQL when you need nested data in a single request. Use REST for simple lookups.