What it does
gh logs in to GitHub once (gh auth login) and then works with the repo of the current folder. You can open and review pull requests, watch and re-run GitHub Actions workflows, read logs, set repository secrets, create releases and call the REST or GraphQL API (gh api). It is also installed on GitHub-hosted runners, so pipelines use it with GH_TOKEN.
How to write it
gh COMMAND SUBCOMMAND [FLAGS]
gh api ENDPOINT [-X METHOD] [-f key=value]Main parts
Commands
authlogin/status- Log in / check the login.
repoclone|create|view|fork- Repositories.
prcreate|list|view|checkout|review|merge- Pull requests.
issuecreate|list|view|close- Issues.
runlist|view|watch|rerun- GitHub Actions runs.
workflowlist|run|enable|disable- Workflows.
secretset|list|delete- Actions secrets.
variableset|list- Actions variables.
releasecreate|list|download- Releases.
apiENDPOINT- Call the REST or GraphQL API.
attestationverify- Check build provenance of an artifact.
rulesetlist|view- Repository rules.
Options 5
Most used
Common flags
Nothing matches this filter.
Examples 11
$ run as a normal user# run as root (or with sudo)
1Log in and check
gh auth login gh auth status2Open a pull request from your branch
git push -u origin feat/add-health-check gh pr create --title "Add /health endpoint" --body "Adds a health check for the load balancer." --base main gh pr view --web3Review and check out a colleague's PR
gh pr list gh pr checkout 42 gh pr diff 42 gh pr review 42 --approve -b "Looks good"4Watch a workflow run and read failed logs
gh run list --limit 5 gh run watch gh run view --log-failed gh run rerun 9483721055 --failed5Start a workflow by hand
gh workflow run deploy.yml -f environment=staging6Set a secret for Actions
gh secret set AZURE_CLIENT_ID --body "00000000-0000-0000-0000-000000000000" gh secret set KUBECONFIG < ~/.kube/lab.config gh secret list7Make a release with files
gh release create v1.4.0 dist/*.tar.gz --title "Shop 1.4.0" --notes-file CHANGELOG.md8Call the API
gh api repos/s0sta/platform/pulls?state=open --jq '.[].title' gh api repos/s0sta/platform/branches/main/protection gh api --paginate orgs/myorg/repos --jq '.[].full_name'9Check Dependabot and code-scanning alerts
gh api repos/s0sta/platform/dependabot/alerts --jq '.[] | [.security_advisory.severity, .dependency.package.name] | @tsv' gh api repos/s0sta/platform/code-scanning/alerts --jq '.[].rule.id'10Verify that an artifact was built by your pipeline
gh attestation verify shop.tar.gz --owner s0sta11Use gh inside a workflow
cat .github/workflows/label.yml steps: - run: gh pr edit ${{ github.event.number }} --add-label "needs-review" env: GH_TOKEN: ${{ github.token }}
Nothing matches this filter.
Watch out
gh auth tokenprints your token; never paste it into logs or chats.- Give CI tokens the smallest
permissions:they need.
Files
~/.config/gh/hosts.yml- Login data.
~/.config/gh/config.yml- Settings and aliases.
Tips to remember
gh alias set prs 'pr list --author @me'makes short commands.
Exit codes
0 | Success. |
1 | Error. |
4 | Login needed. |