GitHub CLI Tutorial: Complete Guide to gh Commands
GitHub CLI Tutorial: Complete Guide to gh Commands
Are you tired of bouncing between browser tabs to merge a pull request or check an issue?GitHubCLI lets you do everything from your terminal without context switching.
What is GitHub CLI?
GitHub CLIis a command-line tool that helps us interact with GitHub directly from the terminal. Instead of opening GitHub in a browser, we can useghcommands to manage repositories, create pull requests, review issues, trigger workflows, and perform other operations without leaving the code environment. Key features of GitHub CLI include:
- GitHub Actions support: Monitor, rerun, and debug workflows with basic commands.
- Custom aliases: Create shorthand commands for repetitive tasks or multi-step scripts.
- Scriptable automation: Chainghcommands into scripts for CI, deployment, or project housekeeping.
- Context awareness: Automatically works with the current repo and branch, minimizing flags and setup.
- Cross-platform support: Works on macOS, Windows, and Linux with consistent behavior.
Before we start using these features, we need to install and set up GitHub CLI on our machine.
How to install and set up GitHub CLI
GitHub CLI supports all major operating systems and is installed in only a couple of steps. Once installed, you’ll connect it to your GitHub account to interact with your repos.
Installing GitHub CLI on Windows
You can install GitHub CLI on Windows using Winget, the Windows Package Manager. Open the command prompt or PowerShell and run:
winget install --id GitHub.cli
Copy to clipboard
winget install --id GitHub.cli
This command tells Winget to fetch and install the official GitHub CLI package.
Note: If you don’t have Winget installed or prefer not to use it, you can download the latest.msiinstaller from the officialGitHub CLI website.
After installation, confirm it’s working by running:
gh --version
Copy to clipboard
gh --version
Installing GitHub CLI on macOS
If you’re on macOS and have Homebrew installed, run:
brew install gh
Copy to clipboard
brew install gh
Then verify the installation:
gh --version
Copy to clipboard
Installing GitHub CLI on Linux
GitHub CLI is available through most major Linux package managers.
For Debian/Ubuntu, use the following command:
sudo apt install gh
Copy to clipboard
sudo apt install gh
For Fedora use:
sudo dnf install gh
Copy to clipboard
sudo dnf install gh
For other distributions, follow themanual installation instructionson GitHub’s official CLI repo.
Authenticating with your GitHub account
Once installed, run the following to authenticate:
gh auth login
Copy to clipboard
gh auth login
The CLI will walk you through the following:
- Selecting GitHub.com or GitHub Enterprise
- Choosing HTTPS or SSH
- Logging in via browser or personal access token
You don’t need to remember any flags, but make sure to follow the prompts.
Verify the authentication status
To make sure everything is working, use:
gh auth status
Copy to clipboard
gh auth status
If everything’s configured correctly, you’ll see confirmation that you’re logged in and Git operations are set up.
Now that we’ve installed GitHub CLI, let’s look at common commands.
Overview of common GitHub CLI commands
Here are some of the most usedghcommands. These are the building blocks of most workflows you’ll use with GitHub CLI. Each command generally follows this structure:
gh [command] [subcommand] [flags/options]
In this syntax,
- gh: This is the GitHub CLI command itself.
- command: This refers to the category of the task you’re performing. For example:repo,issue,pr,run.
- subcommand: The specific action you want to perform within that group. For example:create,clone,view,list,status
- flags/options: Optional arguments that refine the command’s behavior. These usually start with--(like--public,--state closed, etc.)
Here’s a preview of the most frequently used GitHub CLI commands:
- To create a repository:gh repo create
- To list issues:gh issue list
- To create a pull request (PR):gh pr create
To see all available commands and options, we can run:
gh help
Copy to clipboard
gh help
Now that you’ve got the basic structure, let’s see its usage.
Managing repositories with GitHub CLI
Therepogroup in GitHub CLI handles everything related to repositories. This is handy when you’re starting a new project or contributing to someone else’s work. Here are some of the core repository commands you’ll use:
Create a new repository
To create a repository, we use:
gh repo create
Copy to clipboard
gh repo create
This command walks you through an interactive setup where you can:
- Set the repository name
- Choose visibility (public or private)
- Initialize with README, .gitignore, or a license
- Push an existing local repo to GitHub
You can also pass flags for a non-interactive setup:
gh repo create my-project --public --source=. --push
Copy to clipboard
gh repo create my-project --public --source=. --push
Clone a repository
To clone a repository, use:
gh repo clone owner/repo-name
Copy to clipboard
gh repo clone owner/repo-name
This pulls the entire repository to your local machine and sets up the remote automatically.
View repository info
To quickly check details about a repository:
gh repo view
Copy to clipboard
gh repo view
By default, it shows info for the current repo, including description, visibility, and README. Add--webto open it in the browser or--jsonto get structured data.
Delete a repository
Be careful, as this action is permanent and cannot be undone. To delete a repo (that you own):
gh repo delete owner/repo-name
Copy to clipboard
gh repo delete owner/repo-name
You’ll be asked to confirm before deletion.
Working with issues using GitHub CLI
Theissuegroup in GitHub CLI lets you manage GitHub issues, whether you create new ones, review bugs, assign tasks, or leave comments.
Create a new issue
gh issue create
Copy to clipboard
gh issue create
This opens an interactive prompt where you can:
- Enter the title and description
- Add labels
- Assign the issue to someone (including yourself)
- Set a milestone (if available)
To skip the prompts and use flags:
gh issue create --title "Bug: crash on save" --body "Steps to reproduce..." --label bug
Copy to clipboard
gh issue create --title "Bug: crash on save" --body "Steps to reproduce..." --label bug
List issues
gh issue list
Copy to clipboard
gh issue list
This shows all open issues in the current repository. You can filter by state, label, assignee, or author using flags:
gh issue list --label bug --state closed gh issue list --assignee @me
Copy to clipboard
gh issue list --label bug --state closed gh issue list --assignee @me
View a specific issue
gh issue view ISSUE_NUMBER
Copy to clipboard
gh issue view ISSUE_NUMBER
You’ll see the title, body, labels, and comments. Add--commentsto include all discussion threads, or--webto open the issue in your browser.
Managing pull requests with GitHub CLI
Theprgroup in GitHub CLI covers everything you need to work with pull requests. Here are some common operations.
Create a PR
gh pr create
Copy to clipboard
gh pr create
This command prompts you to:
- Choose a base and compare branches
- Add a title and description
- Optionally mark it as a draft
It can also be done non-interactively:
gh pr create --base main --head feature-branch --title "Add login form" --body "Implements login with basic validation"
Copy to clipboard
gh pr create --base main --head feature-branch --title "Add login form" --body "Implements login with basic validation"
List PRs
gh pr list
Copy to clipboard
gh pr list
By default, this shows open PRs in the current repo. This can also filter with:
gh pr list --state closed --author username --label enhancement
Copy to clipboard
gh pr list --state closed --author username --label enhancement
View a PR
gh pr view PR_NUMBER
Copy to clipboard
gh pr view PR_NUMBER
Add--webto open it in the browser, or--commentsto see all discussion inline.
Check PR status
gh pr status
Copy to clipboard
gh pr status
This command is useful when you’re working on a feature branch. It shows:
- Whether there’s an open PR from your branch
- Incoming PRs that need your review
- Review requests assigned to you
Merge a PR
gh pr merge PR_NUMBER
Copy to clipboard
gh pr merge PR_NUMBER
You’ll be prompted to choose between a merge commit, rebase, or squash. You can also specify it with flags:
gh pr merge 17 --squash --delete-branch
Copy to clipboard
gh pr merge 17 --squash --delete-branch
Working with GitHub actions in GitHub CLI
Therungroup in GitHub CLI helps you interact with GitHub Actions workflows directly from the terminal.
List workflows runs
gh run list
Copy to clipboard
gh run list
This shows recent workflow runs for the current repository, including their status (success, failure, in progress). You can add flags to filter by branch, event type, or status:
gh run list --branch main --status failure
Copy to clipboard
gh run list --branch main --status failure
View workflow run details
gh run view RUN_ID
Copy to clipboard
gh run view RUN_ID
You can find theRUN_IDfrom theghrun list output.
View logs
To see logs of a specific run directly in the terminal:
gh run view RUN_ID --log
Copy to clipboard
gh run view RUN_ID --log
This is handy when debugging failed builds or tests without navigating through the UI.
Re-run a workflow
gh run rerun RUN_ID
Copy to clipboard
gh run rerun RUN_ID
You can also re-run the latest failed run:
gh run rerun –failed
Copy to clipboard
gh run rerun –failed
This is especially useful in CI pipelines where flaky tests or race conditions occasionally cause failures.
Customize and automate with aliases and scripts
Aliases that save you time, and scripting that lets you automate repetitive work. Here are some common commands.
Create your shortcuts with aliases
If you find yourself typing the sameghcommand repeatedly, create a custom alias to make your life easier. It has the following syntax:
gh alias set [shortcut] '[full gh command]'
Copy to clipboard
gh alias set [shortcut] '[full gh command]'
For example, this command:
gh alias set myissues 'issue list --author @me --state open'
Copy to clipboard
gh alias set myissues 'issue list --author @me --state open'
Now you can just type:
gh myissues
Copy to clipboard
gh myissues
To see all your aliases:
gh alias list
Copy to clipboard
gh alias list
Aliases can even include flags or parameters. You can get creative with them depending on your workflow.
Automate workflows withJSONand scripts
Mostghcommands support--jsonoutput, which is great for automation and scripting. For example:
gh pr list --state open --json title,author
Copy to clipboard
gh pr list --state open --json title,author
If you want to format the output, usejq:
gh issue list --json number,title --jq '.[] | "\(.number): \(.title)"'
Copy to clipboard
gh issue list --json number,title --jq '.[] | "\(.number): \(.title)"'
This lets you build shell scripts that:
- List PRs and issues
- Parse them
- Trigger CI/CD workflows
- Even auto-generate changelogs
So, with GitHub CLI in your toolkit, you can skip the context switching and get more done right from your terminal. It’s a small shift that can make a big difference.
Conclusion
In this article, we explored how GitHub CLI can simplify and speed up your workflow by bringing GitHub features directly into your terminal. From setting up repositories and managing pull requests to customizing aliases and automating tasks, GitHub CLI gives you the control and flexibility to handle everyday GitHub tasks without ever leaving your command line.
If you’re ready to go deeper and start building real-world projects with Git and GitHub, check out theLearn Git & GitHubcourse on Codecademy.
Frequently asked questions
1. What is GitHub CLI used for?
GitHub CLI (gh) is a command-line tool that lets you interact with GitHub directly from your terminal. It helps create repositories, manage pull requests and issues, view commits, clone projects, and automate workflows.
2. Is GitHub CLI and Git Bash the same?
No, Git Bash is a terminal application for Windows that provides a Bash shell to run Git commands. GitHub CLI is a separate tool that adds GitHub-specific commands (likegh pr create,gh issue list, etc.) to the terminal, working with Git to streamline GitHub operations.
3. How to connect to GitHub via CLI?
To connect, first install GitHub CLI and then rungh auth login. You’ll be guided through a simple authentication flow to sign in with your GitHub account. Once authenticated, you can start using gh commands to interact with your repositories.
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Complete Payment to Unlock Full Guide & Source Code
You are viewing the 30% preview. Join TopCourseAI Pro to unlock the remaining 70% of this article, unblur all code snippets, and gain full access to all 412 technical projects and guides.
Ready to access the complete step-by-step tutorial?
Gain full unhindered access to all 412 technical projects and guides, unblur all source code snippets, and download runnable scripts.
Related Blueprints & Guides
View all 166 →Android Studio Quick-Start Guide
Android Studio often intimidates beginners, but by learning a few handy shortcuts and getting the lay of the land, we can navigate this powerful development environment with ease.
Developer toolsApache Kafka Tutorial: A Complete Guide for Beginners
Apache Kafkais a well-known distributed event streaming platform designed to efficiently manage real-time data feeds. It was originally developed by LinkedIn in 2011 and later open-sourced. Apache Kafka has gained immens...
Developer toolsBuilding Websites with Wix
Creating a website has never been easier. Previously, making a presentable website meant learning HTML, CSS, andJavaScript. The whole process could take weeks, months, or even years before creating a website that you’re...
Ready to build production-grade AI systems?
Enjoyed this guide? Continue exploring our library of 166+ production blueprints, multi-agent swarms, and autonomous agent systems with 100% unblurred, executable source code.