cheat-sheets

GitHub and the gh CLI

gh is GitHub’s official command line tool for repositories, pull requests, issues, Actions and the API. For plain Git commands see Git. For other Git hosts see GitLab and the glab CLI and Gitea (and Forgejo).

Documentation: GitHub CLI manual


Install and Log In #

PLATFORMCOMMAND
Debian / Ubuntuadd the official apt repo from the manual, then sudo apt install gh
macOSbrew install gh (Homebrew)
Windowschoco install gh (Chocolatey) or winget install GitHub.cli
COMMANDDESCRIPTION
gh auth loginLog in (browser or token, choose SSH or HTTPS)
gh auth statusShow the logged-in account and scopes
gh auth setup-gitUse gh as the Git credential helper
gh auth refresh -s workflowAdd a token scope
ssh -T git@github.comTest SSH access

For scripts set GH_TOKEN (and GH_HOST/GH_ENTERPRISE_TOKEN for Enterprise). Use --repo OWNER/REPO (or -R) to run a command against another repository without cloning it.

Repositories #

COMMANDDESCRIPTION
gh repo clone OWNER/REPOClone
gh repo create NAME --private --source=. --pushCreate from the current folder and push
gh repo fork --cloneFork and clone
gh repo view --webOpen in the browser
gh repo list OWNER --limit 50List repositories
gh repo edit --visibility private --accept-visibility-change-consequencesChange settings
gh repo delete OWNER/REPODelete (needs the delete_repo scope)

Pull Requests #

COMMANDDESCRIPTION
gh pr create --fillOpen a PR from the branch using the commit messages
gh pr create --title "T" --body-file body.md --draftOpen a draft with a body from a file
gh pr list --state allList PRs
gh pr view 15 --commentsShow a PR
gh pr checkout 15Check out a PR branch
gh pr diff 15Show the diff
gh pr checks 15 --watchWatch CI
gh pr review 15 --approveReview (--request-changes, --comment)
gh pr merge 15 --squash --delete-branchMerge
gh pr edit 15 --title "T" --body-file body.mdEdit title or description
gh pr ready 15Mark a draft as ready

Issues #

COMMANDDESCRIPTION
gh issue create --title "T" --body "B" --label bugCreate
gh issue list --assignee @meList yours
gh issue view 12Show
gh issue close 12 --comment "Done"Close
gh issue comment 12 --body "text"Comment

Actions and Workflows #

COMMANDDESCRIPTION
gh run list --limit 10Recent runs
gh run view <ID> --log-failedLogs of failed steps
gh run watch <ID>Follow a run
gh run rerun <ID> --failedRe-run failed jobs
gh workflow listList workflows
gh workflow run NAME.yml -f key=valueTrigger a workflow_dispatch workflow
gh secret set NAME --body "value"Set a repository secret
gh variable set NAME --body "value"Set an Actions variable

Releases, API and Extras #

COMMANDDESCRIPTION
gh release create v1.0.0 --generate-notes ./fileCreate a release with assets
gh release listList releases
gh api repos/OWNER/REPOCall the REST API
gh api graphql -f query='{ viewer { login } }'GraphQL
gh api repos/OWNER/REPO/pulls --jq '.[].title'Filter output with a jq expression (jq)
gh alias set co 'pr checkout'Create an alias
gh extension install OWNER/gh-NAMEInstall an extension
gh search repos "topic:homelab" --limit 10Search

Useful GitHub Features #

  • Actions: workflows live in .github/workflows/*.yml. This repo uses one to auto-assign issues.
  • Renovate or Dependabot: automated dependency PRs (renovate.json here).
  • Repository settings as code: the Probot Settings app reads .github/settings.yml.
  • SSH keys and signing: add keys at https://github.com/settings/keys. See OpenSSH Cheat-Sheet.
  • Pages, Releases, Packages (ghcr.io) and Codespaces are built in.

Not Verified #

Commands were written from knowledge of current gh and not run here. Check gh <command> --help for flags in your version.

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.