The GitHub CLI

Intermediate · 42 min

Use gh to create repositories, open and merge pull requests, file issues and watch Actions runs from the terminal, and call any GitHub API endpoint with gh api.

Before thisGitHub accounts, SSH keys and authenticationCreating and publishing a repositoryPull requestsMerge methods: squash, rebase and merge

DevInsideYou · 42 min

The GitHub CLI

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by DevInsideYouReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • gh is GitHub's official command-line tool for the things Git itself does not know about: repositories, pull requests, issues and workflow runs.
  • gh auth login stores a token, and in GitHub Actions or other headless environments you set the GH_TOKEN environment variable instead.
  • A pull request can be created, checked out, inspected and merged without leaving the terminal, using gh pr create, checkout, checks and merge.
  • gh run watch and gh pr checks let you wait on automated checks from the command line, and gh run view --log-failed shows why a step failed.
  • gh api calls any REST or GraphQL endpoint with your authentication, which makes it the escape hatch when no dedicated command exists.

Why a second tool

Git knows about commits, branches and remotes. It does not know what a pull request is, because pull requests are a GitHub feature. Everything in this group so far, opening a PR, merging it, filing an issue, has been done by clicking in a browser. That is fine, until you want to do it ten times, script it, or stay in the terminal you are already in.

The GitHub CLI, gh, fills that gap. It is GitHub’s official command-line client for the service. git talks to your repository. gh talks to GitHub.

Signing in

gh auth login

This walks you through choosing a host and a protocol and then authenticates. By default it opens a browser (--web). You can pass --git-protocol ssh or https to choose how Git will talk to GitHub. If you pick SSH, the docs say gh detects existing keys and offers to create and upload one, which does the SSH setup lesson’s work for you. Web-flow tokens go in your system credential store, and if none is available gh falls back to a plain text file. gh auth status shows where the token lives.

For scripts and CI there is no browser. Set the GH_TOKEN environment variable instead. Inside GitHub Actions the docs show GH_TOKEN: ${{ github.token }} under env. That is the same short-lived workflow token covered in the secrets and contexts lesson.

A day’s work, from the terminal

Here is the notes project’s loop with each step as a command. The flags below are checked against GitHub’s CLI manual, read on 2026-11-23.

# Publish a local project: create the remote, add it and push
gh repo create notes-cli --private --source=. --remote=origin --push

# Branch, work, push, then open a pull request
git switch -c add-tag-filter
gh pr create --title "Add --tag filter to list" --fill --draft

# What is my PR doing?
gh pr checks --watch
gh pr list --author "@me"

# Review someone else's PR locally
gh pr checkout 32

# Merge the current branch's PR with squash, and delete the branch
gh pr merge --squash --delete-branch

Some detail on each.

  • gh repo create. --source points at your local directory, --remote names the remote it adds, and --push uploads your commits. You must pass one of --public, --private or --internal when not using prompts. This is the one-command version of the three commands in creating and publishing a repository.
  • gh pr create. --title and --body set the text, --fill takes them from your commits instead, --draft opens a draft, and --base, --head, --reviewer and --label do what they say. --web opens the browser form instead.
  • gh pr checks. With no argument it uses the current branch’s PR. --watch stays until checks finish, --fail-fast stops at the first failure, and --required shows only the required ones.
  • gh pr checkout 32. It fetches that PR’s branch and checks it out locally, and the docs say it works for PRs from forks by number. That makes local review a one-liner.
  • gh pr merge. --merge, --squash and --rebase choose among the three merge methods. --auto merges once required checks and reviews are satisfied, and --delete-branch removes the branch locally and remotely. --admin bypasses requirements, so treat it with care.

Issues work the same way: gh issue create --title "..." --body "..." --label bug --assignee "@me" files one, and --template "Bug Report" starts from a named template.

Actions from the terminal

When a check fails, you want to know why without opening a browser:

gh run watch              # follow the current run until it completes
gh run view --log-failed  # print the logs of only the failed steps
gh run view --web         # open the run in the browser

gh run watch --exit-status makes the command exit non-zero if the run fails, so you can chain it, as in gh run watch --exit-status && echo ok. The docs note that gh run watch does not work with fine-grained personal access tokens, because those cannot be given the checks:read permission. This matters if you authenticate with a token instead of the browser flow.

The escape hatch: gh api

No CLI covers all of GitHub. gh api sends an authenticated request to any REST endpoint, or to GraphQL.

gh api repos/{owner}/{repo}/releases
gh api repos/{owner}/{repo}/issues/123/comments -f body='Hi from the CLI'
gh api repos/{owner}/{repo}/issues --jq '.[].title'

{owner} and {repo} fill in from the current repository. -f adds a string field, -F a typed one, and --jq filters the JSON. A request with fields switches to POST automatically, and --paginate follows all pages. It is how you script things such as creating a ruleset from a JSON file: the manual shows gh api repos/{owner}/{repo}/rulesets --input file.json.

What I could and could not run

I could not run any gh command. The CLI is not installed in the environment where I wrote this, and I have no GitHub account to sign in with. Every flag above is taken from the official manual pages, and I have not seen their output. Where I describe behaviour, such as --watch staying until checks finish, it is the manual’s description. Run gh <command> --help for the version you have, since flags are added over time.

What the video covers

DevInsideYou’s “#GitHub #CLI - Crash Course” is about 42 minutes and was published around 2020 (YouTube shows “5 years ago”). Its description says it plays with the GitHub CLI and links the project. I could not read a transcript or chapters. This is a compromise. I looked for an English video from an established channel published in 2024 or later and did not find one. Newer English results were small channels with thin descriptions, or about the Copilot CLI instead. This one is older, long and unscripted, and the CLI has gained commands since, so use the manual above for current flags. It is from an established channel with a clear English title and description, and it covers repositories, pull requests and issues. A Spanish-language video I considered, Fazt Code’s, was rejected because its real title is in Spanish. Amitabh Soni’s 2026 video from the first draft is linked under deeper as the shorter, newer alternative, with its thin metadata noted.

Where this fits

That finishes the collaboration group. You can publish, branch, review, merge, protect and script it. The next group covers what GitHub does for you beyond hosting code, starting with turning a repository into a website: GitHub Pages.

Check yourself

  1. 01What can gh do that git cannot?

    Talk to GitHub as a service: create repositories, open and merge pull requests, manage issues and releases, and watch workflow runs. Git only moves commits between repositories.

  2. 02How do you authenticate gh in a script or CI job, where no browser is available?

    Set the GH_TOKEN environment variable to a token. In GitHub Actions you can pass the workflow's token with GH_TOKEN: ${{ github.token }}.

  3. 03How would you merge the current branch's pull request with squash and delete the branch afterwards?

    gh pr merge --squash --delete-branch. Without a number it picks the pull request for the current branch.

  4. 04No gh subcommand exists for an endpoint you need. What do you use?

    gh api <endpoint>, which sends an authenticated request to the REST API, or gh api graphql for GraphQL. -f and -F add parameters.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark