Learn GitHub
$ gh ghautomation.ghcli

GitHub CLI and Developer Tools

6 learning objectives · 9 key terms · 11 commands · 2 diagrams · 5 quiz questions

$objectives

GitHub CLI and Developer Tools

Automation & Security

Module 4 of 5 · lesson 16 in this module

What you'll be able to do

  • Install and authenticate the GitHub CLI
  • Create and clone repositories without leaving the terminal
  • File issues and open pull requests from the command line
  • Inspect workflow runs and logs, and re-run failures
  • Manage releases and query the API with gh api
  • Define aliases for the commands you type most
$concept

The idea, in plain language

The GitHub CLI is a single binary, gh, that wraps the GitHub API in commands shaped like the rest of your terminal workflow. It matters because it removes context switches: you are already in the repository you are thinking about, you already know the branch name, and you already have the issue number in your scrollback. Creating the branch, the commit, the push and the pull request becomes one continuous session instead of a hop into a browser.

Authentication is the one-time setup and it is handled well: gh auth login supports a browser flow or a pasted token, writes to your system credential store, and tells git how to use it for HTTPS operations — so it can also solve the "why does git push keep asking for my password?" problem permanently. Read the scopes it requests; request only what you need.

Most gh commands mirror something you would otherwise do in the web interface, and everything is scriptable. gh pr create takes its title and body from flags or an editor and prints the URL, which means a shell function can open a perfectly formatted pull request every time. gh run watch follows a workflow run live, gh run view --log-failed shows only the failing step's output, gh api reaches anything not yet wrapped in a command, and gh alias set lets you name the five things you type a hundred times a day.

$key terms

Key terms

gh
The GitHub CLI binary; run gh --version to confirm it is installed.
gh auth login
Interactive authentication; stores credentials and configures git to use them over HTTPS.
gh repo create
Creates a repository on GitHub, optionally from an existing local directory.
gh pr create
Opens a pull request from the current branch with title, body, base and reviewers.
gh pr checkout
Checks out someone else’s pull request locally by number — the fastest way to test a contribution.
gh run list / watch
Lists workflow runs and follows a run live in the terminal.
gh api
Direct access to any REST endpoint with automatic authentication and jq-style filtering.
gh alias
A saved shortcut for a gh command with your own arguments.
gh extension
Community-installed subcommands that add new gh capabilities.
$steps

How it works, step by step

  1. 1
    Install the CLI

    Use your platform package manager so upgrades are one command. On Windows, winget; on macOS, Homebrew; on Linux, your distribution’s repository or the official apt repository.

    installbash
    # macOS
    brew install gh
    
    # Windows
    winget install --id GitHub.cli
    
    # Ubuntu / Debian
    sudo apt install gh
  2. 2
    Authenticate once

    The browser flow is easiest; the token flow suits headless machines. Use --scopes to request only what you need, and reply yes when it offers to configure git to use your GitHub credentials for HTTPS.

    authenticatebash
    gh auth login --web --scopes "repo,read:org"
  3. 3
    Create or clone from the terminal

    Create a new repository, or turn a local directory into one and push it without visiting the browser. Cloning by owner/name is shorter than remembering a full URL.

    repositoriesbash
    gh repo create recipe-book --public --source=. --remote=origin --push
    gh repo clone ada/recipe-book
  4. 4
    Work with issues and pull requests

    Create issues, list them, and open a pull request whose body comes from a template. Review someone else’s pull request by checking it out locally.

    issues and PRsbash
    gh issue create --title "Session times out after 5 minutes" --label bug
    gh pr create --base main --title "Extend session timeout" --body "Closes #128"
    gh pr list --state open
    gh pr checkout 231
  5. 5
    Watch your automation from the terminal

    List recent runs, follow one live, and read only the failing step’s log when something breaks — worth more than scrolling the web UI.

    workflow runsbash
    gh run list --limit 5
    gh run watch
    gh run view --log-failed
  6. 6
    Releases and raw API access

    Create releases from tags with generated notes, then use gh api for anything without a dedicated command — filtering the JSON with --jq.

    releases and APIbash
    gh release create v2.5.0 --generate-notes
    gh api repos/:owner/:repo --jq '.stargazers_count'
  7. 7
    Save the commands you type most as aliases

    Aliases turn personal workflows into one-word commands. Shell aliases are not stored in the repository; gh aliases are, in your gh config, and they work from anywhere.

    aliasesbash
    gh alias set co 'pr checkout'
    gh alias set mine 'pr list --author @me'
    gh co 231
$commands

Commands worth memorising

Every block below is copy-ready — use the Copy button, then paste it into your terminal. Expected output is shown underneath so you know what “working” looks like.

install checkbash
gh --version
Output
gh version 2.62.0 (2026-02-11)
https://github.com/cli/cli/releases/latest

If this fails, install updates have not reached your PATH — open a new terminal.

authenticatebash
gh auth login --web
Output
✓ Authentication complete.
✓ Configured git protocol git@github.com.
✓ Logged in as ada

It also offers to configure git credentials, which fixes repeated HTTPS password prompts.

who am Ibash
gh auth status
Output
github.com
  ✓ Logged in to github.com account ada (keyring)
  - Active account: true
  - Token scopes: 'gist', 'read:org', 'repo'

The scopes line is the fastest way to diagnose permission errors.

create a repository from a local folderbash
gh repo create recipe-book --public --source=. --remote=origin --push
Output
✓ Created repository ada/recipe-book on GitHub
✓ Added remote origin
✓ Pushed commits to https://github.com/ada/recipe-book.git

One command replaces creating on GitHub, adding a remote and pushing.

open a pull requestbash
gh pr create --base main --title "Extend session timeout" --body "Closes #128"
Output
https://github.com/ada/app/pull/231

Prints the URL, so your shell can open it or your script can capture it.

review someone else’s changebash
gh pr checkout 231
Output
branch 'fix/session-timeout' set up to track 'origin/fix/session-timeout'.

Checks the pull request out under its own branch name, so you can run and test it locally before approving.

follow a workflow runbash
gh run watch
Output
✓ main Run Tests · 25183446201
  ✓ Set up job
  ✓ Checkout repository
  ✓ Run tests

Follows the most recent run for the current branch and exits with a non-zero status on failure — useful in scripts.

read only the failurebash
gh run view --log-failed

Prints the log of the failing step only. Much faster than paging through a full run log.

releasebash
gh release create v2.5.0 --generate-notes
Output
https://github.com/ada/app/releases/tag/v2.5.0

Creates the release from the existing tag and fills the notes from merged pull requests.

raw APIbash
gh api repos/:owner/:repo/branches/main/protection --jq '.required_pull_request_reviews.required_approving_review_count'
Output
1

:owner and :repo are filled from the current repository, and --jq filters the JSON so scripts stay readable.

alias your habitsbash
gh alias set mine 'pr list --author @me --state open'
Output
Adding alias for mine

Aliases live in your gh config, so they follow you to any machine where you are authenticated.

$diagrams

Workflow diagrams

Diagram 1What the CLI replaces

Same operations, same permissions, one context — the terminal you are already in.

Reading the diagram
  1. Anything the web interface can do, the CLI can do with the same account and permissions.
  2. The advantage is context: branch names, issue numbers and logs are already on screen.
  3. gh api covers features that have no dedicated command yet.
Diagram 2A whole change without leaving the terminal

Two commands create the work item and the review; everything between is ordinary Git.

Reading the diagram
  1. The CLI files the issue with labels and assignees in one quoted command.
  2. Branching, committing and pushing stay in Git — the CLI adds no new concepts there.
  3. gh pr create opens the pull request and prints its URL.
  4. gh pr checks reports the status of required checks, which makes it scriptable as a gate.
$example

Worked example

A complete change managed from the terminal, including watching the checks and merging.

one change, one terminalbash
gh auth status
# ✓ Logged in to github.com account ada (keyring)

gh issue create --title "Session times out after 5 minutes" \
  --body "Expected 30 minutes idle; logged out after ~5." \\
  --label bug --assignee @me --milestone v2.5
# https://github.com/ada/app/issues/128

git switch -c fix/session-timeout
# ... edit auth/session.py ...
git commit -am "Extend idle session timeout to 30 minutes (refs #128)"
git push -u origin fix/session-timeout

gh pr create --base main \
  --title "Extend session idle timeout to 30 minutes" \
  --body "Closes #128. Verified manually with a 31-minute idle test."

gh pr checks --watch
# ✓ test   1m12s
# ✓ lint   22s

gh pr merge --squash --delete-branch
# ✓ Squashed and merged pull request #231
# ✓ Deleted branch fix/session-timeout

git switch main && git pull
gh issue view 128 --json state --jq .state
# CLOSED
Output
Issue filed, branch pushed, pull request reviewed by CI, squash-merged, branch deleted, issue closed — all without opening a browser.
1gh auth status confirms which account and token scopes are active before anything else runs — the first thing to check when a command returns 403.
2gh issue create takes the whole report on the command line. The URL it prints is the one you paste into a chat when someone asks what you are working on.
3Plain Git handles the branch, commit and push; the CLI has not changed anything about the core workflow.
4gh pr create links the issue with a closing keyword, so the tracker updates itself when the change merges.
5gh pr checks --watch blocks until the required checks finish and exits non-zero if any fail, which makes it usable as a gate in a script.
6gh pr merge --squash --delete-branch performs the merge method the team standardised on and cleans the branch in the same command.
7gh issue view with --json and --jq confirms the issue closed, giving a machine-readable audit of the whole loop.
$use cases

Where you'll use it

▸Scripting repository creation for a template or a new project on day one.
▸Opening a well-formatted pull request from a shell function so every review starts with the same structure.
▸Checking out a contributor’s pull request locally to run the tests before approving it.
▸Watching CI in the terminal while you keep working, instead of refreshing a browser tab.
▸Automating a release from a tag inside an Actions workflow, using the preinstalled gh.
▸Pulling values out of the GitHub API with gh api --jq for dashboards and scripts.
$mistakes

Common mistakes

  • Using a token with more scopes than needed because the default was accepted without reading.
  • Assuming gh replaces Git — it wraps GitHub; commits and branches are still Git.
  • Checking out pull requests for untrusted forks and running their code without reading it first.
  • Letting gh create repositories with unexpected visibility because the default was not specified.
  • Building fragile shell pipelines around gh api without --jq, then parsing JSON with grep.
  • Storing long-lived tokens in shell profiles when gh auth login already manages them securely.
$best practices

Best practices

  • Authenticate with gh auth login and let it configure git credential handling for HTTPS.
  • Request the minimum scopes and re-authenticate when you need more.
  • Use gh pr create with a template or heredoc so descriptions stay consistent.
  • Watch runs with gh run watch and read failures with gh run view --log-failed.
  • Save your five most-typed commands as gh aliases.
  • Use gh api for anything the CLI does not wrap, with --jq for readable output.
  • Treat gh output as an interface: it prints URLs and exits with meaningful status codes, so it composes with scripts.
$exercise

Hands-on exercise

Do a whole change without a browser

File, fix, review and merge from one terminal session.

  1. Run gh --version and gh auth status, and note the token scopes reported.
  2. In a practice repository, file an issue with gh issue create including a label and an assignee.
  3. Create a branch, make the change, commit, and push. Then open the pull request with gh pr create, linking the issue with a closing keyword.
  4. Run gh pr checks --watch and read the result. If checks fail, use gh run view --log-failed to find the cause.
  5. Merge with gh pr merge --squash --delete-branch and confirm the issue closed with gh issue view <number> --json state.
  6. Create an alias for the command you found yourself typing most in this exercise, and use it once.

💡 Hint: gh commands infer the repository from the current directory’s git remote, so run them from inside the clone — or pass --repo owner/name explicitly.

Done when: You filed an issue, opened and merged a pull request, and closed the issue, all without opening the GitHub website.

$quiz

Checkpoint quiz

0 of 5 answered · 0 correct

  1. Multiple choiceQ1

    What does gh auth login configure besides the CLI itself?

  2. Command outputQ2

    What does this command print for the current repository?

    gh api repos/:owner/:repo --jq '.stargazers_count'
  3. TroubleshootingQ3

    You need to verify a contributor’s pull request locally before approving it. Which command?

  4. True or falseQ4

    True or false: the GitHub CLI can be used inside GitHub Actions workflows, so a workflow can create releases without extra setup.

  5. Multiple choiceQ5

    Which gh command shows only the failing step’s output of a workflow run?

$summary

Module summary

  • gh wraps the GitHub API in terminal-native commands with the same account and permissions as the web interface.
  • gh auth login handles credentials and can configure git for HTTPS, removing repeated password prompts.
  • gh repo create, gh pr create, gh issue create and gh release create cover the everyday operations.
  • gh pr checkout <number> is the fastest way to test a contribution locally.
  • gh run list, gh run watch and gh run view --log-failed make CI observable from the terminal.
  • gh api reaches anything unwrapped, and --jq keeps script output readable.
  • Aliases turn your most-typed commands into one word.

Next up: Tools are in place; Module 14 is about the conventions that make a repository a pleasant place to work: naming, templates, ownership, versioning and review culture.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Put the GitHub course to work: open one of the company-wise repositories and follow the workflow you just learned, branch to release.