--- name: gh-cli description: "Use when performing any GitHub operation from the command line with the gh CLI: creating PRs, managing issues, checking Actions status, working with releases, gists, codespaces, or organizations." --- # GitHub CLI (`gh`) Comprehensive reference for GitHub CLI (`gh`) - work seamlessly with GitHub from the command line. **Version:** 2.85.0 (current as of January 2026) ## Prerequisites ### Installation ```bash # macOS brew install gh # Linux curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list >/dev/null sudo apt update sudo apt install gh # Windows winget install --id GitHub.cli # Verify installation gh --version ``` ### Authentication ```bash # Interactive login (default: github.com) gh auth login # Login with specific hostname gh auth login --hostname enterprise.internal # Login with token gh auth login --with-token 100)' # Complex queries gh issue list --json number,title,labels \ --jq '.[] | {number, title: .title, tags: [.labels[].name]}' ``` ### Template Output ```bash # Custom template gh repo view \ --template '{{.name}}: {{.description}}' # Multiline template gh pr view 123 \ --template 'Title: {{.title}} Author: {{.author.login}} State: {{.state}} ' ``` ## Common Workflows ### Create PR from Issue ```bash # Create branch from issue gh issue develop 123 --branch feature/issue-123 # Make changes, commit, push git add . git commit -m "Fix issue #123" git push # Create PR linking to issue gh pr create --title "Fix #123" --body "Closes #123" ``` ### Bulk Operations ```bash # Close multiple issues gh issue list --search "label:stale" \ --json number \ --jq '.[].number' | xargs -I {} gh issue close {} --comment "Closing as stale" # Add label to multiple PRs gh pr list --search "review:required" \ --json number \ --jq '.[].number' | xargs -I {} gh pr edit {} --add-label needs-review ``` ### Repository Setup Workflow ```bash # Create repository with initial setup gh repo create my-project --public \ --description "My awesome project" \ --clone \ --gitignore python \ --license mit cd my-project # Set up branches git checkout -b develop git push -u origin develop # Create labels gh label create bug --color "d73a4a" --description "Bug report" gh label create enhancement --color "a2eeef" --description "Feature request" gh label create documentation --color "0075ca" --description "Documentation" ``` ### CI/CD Workflow ```bash # Run workflow and wait RUN_ID=$(gh workflow run ci.yml --ref main --jq '.databaseId') # Watch the run gh run watch "$RUN_ID" # Download artifacts on completion gh run download "$RUN_ID" --dir ./artifacts ``` ### Fork Sync Workflow ```bash # Fork repository gh repo fork original/repo --clone cd repo # Add upstream remote git remote add upstream https://github.com/original/repo.git # Sync fork gh repo sync # Or manual sync git fetch upstream git checkout main git merge upstream/main git push origin main ``` ## Best Practices 1. **Authentication**: Use environment variables for automation ```bash export GH_TOKEN=$(gh auth token) ``` 2. **Default Repository**: Set default to avoid repetition ```bash gh repo set-default owner/repo ``` 3. **JSON Parsing**: Use jq for complex data extraction ```bash gh pr list --json number,title --jq '.[] | select(.title | contains("fix"))' ``` 4. **Pagination**: Use --paginate for large result sets ```bash gh issue list --state all --paginate ``` 5. **Caching**: Use cache control for frequently accessed data ```bash gh api /user --cache force ``` ## Getting Help ```bash # General help gh --help # Command help gh pr --help gh issue create --help # Help topics gh help formatting gh help environment gh help exit-codes gh help accessibility ``` ## References - Official Manual: - GitHub Docs: - REST API: - GraphQL API: