π» Productivity with GitHub CLI (gh) & APIs
NOTE
The GitHub CLI (
gh) brings the entire GitHub experience directly to the terminal, allowing engineers to create Pull Requests, review code, configure secrets, and manage CI/CD runs without context switching.
β‘ 1. Essential GitHub CLI (gh) Commands
1.1. Authentication & Configuration
# Secure interactive login via browser or token
gh auth login
# Check authentication status
gh auth status
# Set default editor
gh config set editor "nano"1.2. Pull Request Management in Terminal
# Create a Pull Request with interactive prompts
gh pr create --title "feat(auth): add JWT middleware" --body "Implements JWT auth."
# Open PR as a Draft
gh pr create --draft --title "WIP: database refactor"
# List open PRs
gh pr list
# Checkout a PR branch locally
gh pr checkout 42
# View diff and CI status checks
gh pr diff 42
gh pr checks 42
# Review and merge via CLI
gh pr review 42 --approve -b "LGTM! Tested and verified."
gh pr merge 42 --squash --delete-branch1.3. Workflow Management
# Trigger a workflow manually
gh workflow run deploy-gh-pages.yaml
# Watch execution in real-time
gh run watch1.4. Managing Secrets & Variables
# Set repository secret via stdin
echo "my_api_key" | gh secret set SNYK_TOKEN
# Set environment variable
gh variable set BASE_URL --body "https://pedroiff0.github.io/guia-github"π 2. GitHub REST & GraphQL APIs
Querying via gh api:
# Fetch repository summary in JSON
gh api repos/:owner/:repo | jq '{name: .name, stars: .stargazers_count, forks: .forks_count}'GraphQL Query Example:
gh api graphql -f query='
query {
repository(owner: "pedroiff0", name: "guia-github") {
name
stargazerCount
pullRequests(states: OPEN, first: 5) {
nodes {
number
title
}
}
}
}
'π Official Documentation & References
- π Git SCM Official Documentation β Official Pro Git book and command manual.
- π GitHub Docs β Official guides on GitHub Actions, PRs, Security, and REST/GraphQL APIs.
- π¦ Conventional Commits 1.0.0 Specification β Official specification.
- π‘οΈ SonarCloud Documentation & Snyk Docs β Official SAST & SCA security docs.
π Second Brain Links
- Trigger pipelines configured in GitHub Actions & CI/CD.
- Back to the main hub in GitHub & Git Ecosystem.