Introduction
GitHub CLI (gh) is a powerful command-line tool that brings the full power of GitHub to your terminal. Whether you’re managing repositories, creating pull requests, or tracking issues, GitHub CLI streamlines workflows, making it an indispensable tool for developers. In this guide, we will cover the most commonly used gh commands, focusing on auth, repo, pr, issue, release, browse, gist, and copilot. We’ll also touch on a few additional frequently used commands to help you maximize your productivity.
Why Use GitHub CLI?
GitHub CLI simplifies development workflows by allowing you to interact with GitHub directly from your terminal. This eliminates the need to switch between your terminal and browser, saving time and improving efficiency. With GitHub CLI, you can:
- Authenticate and manage GitHub accounts.
- Create and manage repositories.
- Open and review pull requests.
- Track and resolve issues.
- Publish releases.
- Share code snippets through gists.
- Leverage AI-powered coding assistance with GitHub Copilot.
Getting Started with GitHub CLI
Before diving into the essential commands, you need to install GitHub CLI on your system. You can download the latest version of GitHub CLI from the official website. Once installed, you can start using the gh command to interact with GitHub from your terminal.
1. Authentication with gh auth
The gh auth command is your starting point with GitHub CLI. It allows you to log in, log out, and manage authentication tokens.
Common gh auth Commands:
-
Login to GitHub:
gh auth loginFollow the prompts to authenticate with your GitHub account. You can choose between HTTPS or SSH for authentication.
-
Check Authentication Status:
gh auth statusVerify if you’re logged in and see details about your authentication method.
-
Logout:
gh auth logoutLogs you out of your GitHub account.
2. Repository Management with gh repo
The gh repo command allows you to create, clone, fork, and manage repositories directly from your terminal.
Common gh repo Commands:
-
Clone a Repository:
gh repo clone owner/repo-nameClones a repository to your local machine.
-
Create a New Repository:
gh repo createFollow the prompts to create a new repository on GitHub.
-
View Repository Details:
gh repo viewDisplays information about the current repository.
-
Fork a Repository:
gh repo fork owner/repo-nameCreates a fork of the specified repository.
3. Pull Requests with gh pr
Pull requests are at the heart of collaboration on GitHub. The gh pr command lets you create, view, and manage pull requests with ease.
Common gh pr Commands:
-
Create a New Pull Request:
gh pr createOpens an interactive prompt to create a pull request for your current branch.
-
View Pull Requests:
gh pr listLists all open pull requests in the repository.
-
Checkout a Pull Request:
gh pr checkout pr-numberChecks out the branch associated with the specified pull request.
-
Merge a Pull Request:
gh pr merge pr-numberMerges the specified pull request.
4. Issue Tracking with gh issue
The gh issue command helps you manage issues, a core part of project tracking and collaboration on GitHub.
Common gh issue Commands:
-
Create a New Issue:
gh issue createOpens an interactive prompt to create a new issue.
-
List Issues:
gh issue listLists all open issues in the repository.
-
View Issue Details:
gh issue view issue-numberDisplays detailed information about a specific issue.
-
Close an Issue:
gh issue close issue-numberCloses the specified issue.
5. Releases with gh release
The gh release command allows you to manage releases, which are used to package and distribute project versions.
Common gh release Commands:
-
Create a New Release:
gh release create tag-nameCreates a new release with the specified tag name.
-
List Releases:
gh release listLists all releases in the repository.
-
View Release Details:
gh release view tag-nameDisplays details about the specified release.
-
Delete a Release:
gh release delete tag-nameDeletes the specified release.
6. Browsing with gh browse
The gh browse command is a quick way to open the current repository, issues, or pull requests in your default web browser.
Common gh browse Commands:
-
Open the Current Repository in Browser:
gh browseOpens the repository page in your default web browser.
-
Open a Specific Issue or Pull Request:
gh browse issue-numberOpens the specified issue or pull request in your browser.
7. Sharing Code with gh gist
The gh gist command is used to create and manage gists, which are simple ways to share code snippets.
Common gh gist Commands:
-
Create a New Gist:
gh gist create file-nameCreates a new gist from the specified file.
-
List Your Gists:
gh gist listLists all your gists.
-
View Gist Details:
gh gist view gist-idDisplays details about a specific gist.
-
Delete a Gist:
gh gist delete gist-idDeletes the specified gist.
8. AI-Powered Assistance with gh copilot
GitHub Copilot is an AI-powered coding assistant that helps you write code faster and more efficiently. The gh copilot command manages Copilot settings and access.
Common gh copilot Commands:
-
Enable Copilot:
gh copilot enableEnables GitHub Copilot for your account.
-
Disable Copilot:
gh copilot disableDisables GitHub Copilot.
-
Check Copilot Status:
gh copilot statusDisplays the current status of GitHub Copilot.
Frequently Used Additional Commands
Here are a few more gh commands that developers often use:
-
View Notifications:
gh notification listLists your GitHub notifications.
-
Check GitHub CLI Version:
gh --versionDisplays the installed version of GitHub CLI.
-
Run Workflows:
gh workflow run workflow-nameTriggers a GitHub Actions workflow.
-
List Workflow Runs:
gh run listLists recent workflow runs in the repository.
Final Thoughts
GitHub CLI (gh) is a game-changer for developers who want to optimize their workflows. By mastering these essential commands—auth, repo, pr, issue, release, browse, gist, and copilot—you can manage your GitHub projects more efficiently and focus on what really matters: writing great code.