Github CLI (gh)

From Ilianko
Revision as of 07:19, 27 October 2026 by Anko (talk | contribs) (→Common Commands)

GitHub CLI (gh) is GitHub's official command-line tool that brings pull requests, issues, GitHub Actions workflows, releases, gists, and other GitHub features directly to the terminal.

Overview

gh allows developers to interact with GitHub.com and GitHub Enterprise Server without leaving the command line. It automatically detects the current Git repository context, handles authentication and SSH/HTTPS Git credential configuration, and provides both interactive prompts and scriptable flags (including JSON output with --json and --jq).

Installation

  • Windows (winget / choco / scoop):
 winget install --id GitHub.cli
 choco install gh
  • macOS (Homebrew):
 brew install gh
  • Linux (Debian / Ubuntu):
 sudo apt update
 sudo apt install gh

Authentication

 gh auth login       # Interactive login to GitHub.com or GitHub Enterprise
 gh auth status      # View active authentication state
 gh auth setup-git   # Configure git to use gh as a credential helper
 gh auth token       # Print the current authentication token

Common Commands

<code>
# Check installation
gh --version

# Authenticate
gh auth login

# Check authentication
gh auth status

# Clone a repository
gh repo clone OWNER/REPO

# Create a repository
gh repo create

# List repositories
gh repo list

# Open current repo in browser
gh repo view --web
</code>

Repositories (gh repo)

 gh repo clone OWNER/REPO            # Clone a repository
 gh repo create                      # Create a new repository on GitHub
 gh repo fork                        # Fork an existing repository
 gh repo view --web                  # Open the repository in the browser

Pull Requests (gh pr)

 gh pr list                          # List open pull requests
 gh pr create --fill                 # Create a pull request using commit info
 gh pr checkout NUMBER               # Check out a pull request branch locally
 gh pr status                        # Show status of relevant pull requests
 gh pr checks                        # View CI/CD check statuses for a PR
 gh pr review --approve              # Approve the current pull request
 gh pr merge --squash --delete-branch # Squash-merge and delete branch

Issues (gh issue)

 gh issue list                       # List issues in the repository
 gh issue create                     # Create a new issue interactively
 gh issue view NUMBER               # View issue details and comments
 gh issue develop NUMBER --checkout  # Create and check out a branch linked to an issue
 gh issue close NUMBER               # Close an issue

GitHub Actions (gh run and gh workflow)

 gh workflow list                    # List workflows in the repository
 gh workflow run WORKFLOW_NAME       # Trigger a workflow_dispatch event
 gh run list                         # List recent workflow runs
 gh run watch                        # Watch an in-progress workflow run live
 gh run view --log-failed            # View logs for failed steps in a run

Releases, Gists and Codespaces

 gh release list                     # List repository releases
 gh release create v1.0.0 --generate-notes # Create a release with auto-generated notes
 gh gist create FILE                 # Create a new GitHub Gist
 gh codespace list                   # List active GitHub Codespaces

update

Scripting, REST/GraphQL API and Extensions

  • Structured JSON and jq filtering:
 gh pr list --json number,title,author
  • Direct GitHub API requests (gh api):
 gh api repos/{owner}/{repo}/releases/latest
 gh api graphql -f query='{ viewer { login } }'
  • Aliases and Extensions:
 gh alias set co 'pr checkout'
 gh extension search
 gh extension install dlvhdr/gh-dash

External Links