15. GitHub CLI: Automating GitHub from the Terminal
The GitHub CLI (gh) is a powerful tool for interacting with GitHub from the command line. It enables you to create repositories, manage issues, review pull requests, and run GitHub Actions — all from your terminal. This module covers the essential gh commands. gh brings GitHub to your terminal, speeding up workflows and enabling scripting.
The key insight is that gh brings GitHub to your terminal. You can perform almost any GitHub operation without leaving the command line. This speeds up workflows and enables scripting. Understanding gh is essential for power users. gh is the standard tool for CLI interactions with GitHub. It's used by developers and CI/CD pipelines. Mastering gh makes you more productive.
This module covers installation, authentication, and common gh commands: gh repo, gh pr, gh issue, gh run, and gh alias.
Getting Started with gh
Install the GitHub CLI and authenticate. gh provides a unified interface to GitHub. After authentication, you can perform actions without a browser. gh is the standard tool for CLI interactions with GitHub. It's used by developers and CI/CD pipelines. Mastering gh makes you more productive.
gh is essential for automating GitHub workflows. You can create PRs, review code, and manage issues without leaving the terminal. This is especially useful for power users who live in the terminal.
Common gh Commands
gh provides commands for every GitHub operation. Repo commands: create, clone, fork. PR commands: create, list, checkout, view, review. Issue commands: create, list, close. Run commands: list, view, logs. These commands are the most frequently used gh operations. They cover the core GitHub workflows. Learning these commands saves time and reduces context switching.
The most common gh commands are for pull requests. You can create a PR, list PRs, check out a PR locally, and review a PR without leaving the terminal. This speeds up the development workflow significantly.
Advanced gh Usage
Advanced gh usage includes: aliases for common commands, scripting gh in shell scripts, and using gh in CI/CD. gh can be used for automation and integration. gh is scriptable — you can use it in shell scripts. This enables complex automation. gh is also used in GitHub Actions for advanced workflows.
gh aliases let you create shortcuts for common commands. This saves typing and makes you more efficient. gh is also useful in scripts for automating GitHub operations. For example, you can create a script that automatically creates a PR for every feature branch.
Step-by-Step: Using GitHub CLI
Follow these steps to get started with the GitHub CLI and automate your GitHub workflow.
Step 1: Install gh
macOS: brew install gh
Linux: sudo apt install gh
Windows: winget install --id GitHub.cli
Step 2: Authenticategh auth login
Follow the prompts to authenticate with your GitHub account. You'll need a personal access token.
Step 3: Create a repositorygh repo create my-repo --public --description <strong><code>My new repo
This creates a repository on GitHub without opening the browser.
Step 4: Create a pull requestgh pr create --title <strong><code>Add feature --body Description of changes
This creates a PR from your current branch. You can also specify the base branch.
Step 5: List open issuesgh issue list --state open
This shows all open issues in the current repository.
Step 6: Create an aliasgh alias set p <strong><code>pr list
Now you can use gh p instead of gh pr list.
Step 7: View workflow logsgh run listgh run view <run-id>gh run logs <run-id>
Try It Yourself: Create a script that automatically creates a PR for every feature branch. Use gh in a CI/CD pipeline to comment on PRs.
Knowledge Check
Ready to test your understanding of 15. GitHub CLI: Automating GitHub from the Terminal?