GitHub CLI Cheatsheet
The gh CLI is GitHub’s official command-line tool. It wraps the GitHub API and
lets you do everything the browser can do from your terminal — create issues, open
PRs, check CI status, manage releases, and more. It’s introduced in Module 07
and used throughout the rest of the course.
Authentication
# Authenticate with GitHub (interactive — opens browser)gh auth login
# Authenticate using a token (for CI or scripted use)gh auth login --with-token < token.txt
# Check current authentication statusgh auth status
# Log outgh auth logout
# Refresh auth token (if permissions have changed)gh auth refresh --scopes repo,read:packagesRepositories
# Clone a repositorygh repo clone owner/repo
# Fork a repo and clone it locally in one stepgh repo fork owner/repo --clone --remote
# Sync your fork with upstreamgh repo sync # syncs current repo's forkgh repo sync YOUR-USERNAME/repo \ --source fischer3-net/git-github-security-learning # explicit source
# View repository infogh repo viewgh repo view owner/repo
# Open the repository in your browsergh repo view --webgh repo view owner/repo --web
# List your repositoriesgh repo listgh repo list --limit 20 --language python
# Create a new repositorygh repo create my-new-repo --publicgh repo create my-new-repo --private --cloneIssues
# List open issuesgh issue list
# Filter by label, assignee, or search termgh issue list --label "feat"gh issue list --assignee "@me"gh issue list --search "Calculate Agent"gh issue list --label "feat" --label "starter-project"
# View a specific issuegh issue view 42gh issue view 42 --web # open in browser
# Create an issue interactivelygh issue create
# Create an issue with flagsgh issue create \ --title "feat: implement Calculate Agent" \ --body "Implementation of the Calculate Specialist Agent." \ --label "feat,starter-project"
# Edit an issuegh issue edit 42 --title "Updated title"gh issue edit 42 --add-label "in-progress"gh issue edit 42 --add-assignee "@me"gh issue edit 42 --remove-label "needs-triage"
# Close and reopengh issue close 42gh issue close 42 --comment "Fixed in PR #17"gh issue reopen 42
# Output as JSON (useful for scripting)gh issue list --search "Calculate" --json number,titlegh issue list --search "Calculate" --json number --jq '.[0].number'Pull Requests
# List open PRsgh pr listgh pr list --label "feat"gh pr list --assignee "@me"gh pr list --state merged
# View a PRgh pr view 17gh pr view 17 --web # open in browser
# Create a PR interactivelygh pr create
# Create a PR with flagsgh pr create \ --title "feat(calculate-agent): implement safe arithmetic evaluator" \ --body "Implements the Calculate Agent using AST-based parsing. Closes #42." \ --label "feat,starter-project"
# Create a draft PRgh pr create --draft \ --title "WIP: calculate agent" \ --body "Work in progress — not ready for review."
# Mark a draft PR as ready for reviewgh pr readygh pr ready 17
# Check out a PR's branch locally (works with forks too)gh pr checkout 17
# Check CI status on the current PRgh pr checksgh pr checks 17
# Review a PRgh pr review 17 --approvegh pr review 17 --approve --body "Tested locally — LGTM."gh pr review 17 --request-changes \ --body "The division by zero case returns a 500. Return AgentResponse.error() instead."gh pr review 17 --comment --body "Left a question inline."
# Merge a PRgh pr merge 17 # interactivegh pr merge 17 --merge # merge commitgh pr merge 17 --squash # squash and mergegh pr merge 17 --rebase # rebase and mergegh pr merge 17 --squash --delete-branch # merge and clean up branch
# Edit a PRgh pr edit 17 --title "Updated title"gh pr edit 17 --add-label "needs-review"gh pr edit 17 --add-reviewer username
# Close a PR without merginggh pr close 17
# Diff a PRgh pr diff 17CI / Workflows
# List all workflows in the repositorygh workflow list
# View a specific workflowgh workflow view ci.yml
# Trigger a workflow manually (requires workflow_dispatch trigger)gh workflow run ci.ymlgh workflow run ci.yml --ref maingh workflow run ci.yml --field target=python
# List recent workflow runsgh run listgh run list --workflow=ci.ymlgh run list --workflow=ci.yml --limit 5gh run list --status failure
# View a specific rungh run viewgh run view <run-id>gh run view <run-id> --web # open in browser
# Watch a run live in the terminalgh run watchgh run watch <run-id>
# View logs for a rungh run view <run-id> --loggh run view <run-id> --log-failed # only failed steps
# Download artifacts from a rungh run download <run-id>gh run download <run-id> --name test-results
# Re-run a failed rungh run rerun <run-id>gh run rerun <run-id> --failed-only # only re-run failed jobsReleases
# List releasesgh release list
# View a releasegh release view v1.0.0gh release view v1.0.0 --web # open in browser
# Create a release manually (the release.yml workflow does this automatically)gh release create v1.0.0 \ --title "v1.0.0" \ --generate-notes \ --latest
# Create a pre-releasegh release create v1.1.0-beta.1 \ --prerelease \ --title "v1.1.0 Beta 1" \ --generate-notes
# Upload an artifact to an existing releasegh release upload v1.0.0 sbom.spdx.json
# Download release artifactsgh release download v1.0.0gh release download v1.0.0 --pattern "*.json"
# Delete a release (does not delete the underlying tag)gh release delete v1.0.0Attestation
Used in Module 08 to verify that a container image was built by the repository’s CI pipeline and hasn’t been tampered with.
# Verify a container image's provenancegh attestation verify \ oci://ghcr.io/YOUR-USERNAME/git-github-security-learning/orchestrator-python:v1.0.0 \ --repo YOUR-USERNAME/git-github-security-learning
# Verify against a specific workflowgh attestation verify \ oci://ghcr.io/YOUR-USERNAME/git-github-security-learning/orchestrator-python:v1.0.0 \ --repo YOUR-USERNAME/git-github-security-learning \ --signer-workflow .github/workflows/release.yml
# Download the attestation bundle for offline inspectiongh attestation download \ oci://ghcr.io/YOUR-USERNAME/git-github-security-learning/orchestrator-python:v1.0.0 \ --repo YOUR-USERNAME/git-github-security-learningA successful verification output looks like:
Loaded digest sha256:abc123... for oci://ghcr.io/...Attestation verified for digest sha256:abc123...
The following policy criteria were satisfied:✓ OIDC Issuer matches expected issuer✓ Source Repository URI matches✓ Source Repository Ref matches refs/tags/v1.0.0✓ Runner Environment matches GitHub-hosted runnerCodespaces
# Create a Codespace for the current repogh codespace create
# Create a Codespace for a specific repogh codespace create --repo YOUR-USERNAME/git-github-security-learning
# List your Codespacesgh codespace list
# SSH into a running Codespacegh codespace ssh
# Open a Codespace in VS Codegh codespace code
# Stop a Codespacegh codespace stop
# Delete a Codespacegh codespace deleteSecrets
Used when configuring GitHub Actions secrets (Module 05 and 06).
# List repository secrets (names only — values are never shown)gh secret list
# Set a repository secretgh secret set MY_API_KEY
# Set a secret from a filegh secret set MY_API_KEY < key.txt
# Set a secret with a value inline (avoid — shell history)gh secret set MY_API_KEY --body "value"
# Delete a secretgh secret delete MY_API_KEYGitHub API (Advanced)
gh api gives direct access to the GitHub REST API — useful for scripting
and automation not covered by other gh commands.
# Get repository informationgh api repos/owner/repo
# List packages for a user (used in Module 08)gh api /users/YOUR-USERNAME/packages/container \ --jq '.[].name'
# Create an issue via the APIgh api repos/owner/repo/issues \ --method POST \ --field title="My issue" \ --field body="Issue body"
# Use jq to extract specific fieldsgh api repos/owner/repo/releases \ --jq '.[0].tag_name'Useful Patterns
Open the current PR in the browser
gh pr view --webCheck CI before requesting review
gh pr checksSync fork and update your branch
# Sync fork's main with upstreamgh repo sync
# Then rebase your branchgit fetch origingit rebase origin/maingit push --force-with-leaseCreate an issue and immediately assign it to yourself
gh issue create \ --title "feat: implement Calculate Agent" \ --label "feat" | xargs gh issue edit --add-assignee "@me"
# Or in two steps:gh issue create --title "..." --label "feat"gh issue edit <number> --add-assignee "@me"Watch a triggered workflow run until it finishes
gh workflow run ci.yml && gh run watchQuick Reference
| Task | Command |
|---|---|
| Auth status | gh auth status |
| Clone a repo | gh repo clone owner/repo |
| Fork and clone | gh repo fork owner/repo --clone --remote |
| Sync fork | gh repo sync |
| List issues | gh issue list |
| Create issue | gh issue create |
| List PRs | gh pr list |
| Create PR | gh pr create |
| Create draft PR | gh pr create --draft |
| Check out a PR | gh pr checkout 17 |
| Check PR CI | gh pr checks |
| Approve a PR | gh pr review 17 --approve |
| Merge a PR | gh pr merge 17 --squash --delete-branch |
| Trigger workflow | gh workflow run ci.yml |
| Watch run live | gh run watch |
| View run failures | gh run view --log-failed |
| List releases | gh release list |
| View release | gh release view v1.0.0 |
| Verify attestation | gh attestation verify oci://ghcr.io/... |
| List secrets | gh secret list |
| Set a secret | gh secret set SECRET_NAME |
| Open in browser | gh <command> --web |
Related
- Git Command Cheatsheet — local Git operations that complement the
ghCLI - Module 07 · Collaboration at Scale — where
ghis introduced and used throughout - Module 08 · Packages, Releases & Pages —
gh release,gh run, andgh attestation