Skip to content

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 login

Verify authentication status:

sh
gh auth status

Repository setup ​

Clone ​

sh
gh repo clone owner/repo

Create ​

sh
gh repo create my-repo --public --clone

Fork and clone ​

sh
gh repo fork owner/repo --clone

Common operations ​

View repository info ​

sh
gh repo view owner/repo

Sync fork with upstream ​

sh
gh repo sync owner/repo

List repositories ​

sh
gh repo list owner --limit 50

Set default repository ​

When working outside a Git repo or when the remote is ambiguous:

sh
gh repo set-default owner/repo

Releases ​

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

Upload assets to a release ​

sh
gh release upload v1.0.0 ./dist/artifact.zip

Repository settings via CLI ​

Set topics ​

sh
gh repo edit --add-topic "javascript,web-components"

Configure default branch ​

sh
gh repo edit --default-branch main

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

JSON filtering ​

Use --jq to extract specific fields:

sh
gh api repos/owner/repo --jq '.default_branch'

Trade-offs ​

  • gh CLI vs raw git: Use gh for GitHub-specific operations (issues, PRs, releases, API). Use git for standard version control (commit, branch, merge).
  • gh api vs built-in commands: Prefer built-in commands when available (gh issue list over gh api repos/.../issues). Fall back to gh api for advanced queries or missing features.
  • GraphQL vs REST: Use GraphQL when you need nested data in a single request. Use REST for simple lookups.