Open In App

What is GitHub CLI?

Last Updated : 29 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

GitHub CLI (Command Line Interface) is a powerful tool developed by GitHub that allows developers to interact with GitHub directly from the terminal. It provides a simple way to perform many GitHub tasks without leaving the command line interface, such as managing repositories, handling pull requests and issues, working with GitHub Actions, and more.

Overview of GitHub CLI

GitHub is used to bridge the gap between GitHub's web interface and our local environment. We can perform various tasks such as creating issues, managing repositories, or even checking the status of your GitHub Actions workflows using GitHub CLI. We can say that, we can perform almost all the tasks that we do on the GitHub website.

Key Features of GitHub CLI

  • Repository Management: Easily create, clone, view, and manage repositories.
  • Pull Requests and Issues: Manage pull requests and issues directly from the terminal, including creating, merging, and listing them.
  • GitHub Actions: Interact with workflows and manage workflow runs.
  • Authentication: Provides a secure way to authenticate with your GitHub account, supporting SSH keys, tokens, and OAuth.
  • Custom Scripting: Enables the creation of custom scripts and aliases to automate repetitive tasks and streamline development processes.

Benefits of Using GitHub CLI

Suppose we are working on a project, and we need to create a new issue on GitHub. Normally, we would switch to our browser, log in to GitHub, navigate to the repository, click on the “Issues” tab, and then click “New Issue.” With GitHub CLI, we can do all of this by typing a single command—without ever leaving your terminal. This makes our workflow faster and saves time.

Installation and Setup

To install GitHub CLI on Windows, we can use the winget package manager. winget is a command-line tool that allows us to install software easily.

Installing GitHub CLI on Windows, macOS, and Linux

Windows:

We run the command given below:

winget install --id GitHub.cli

where

  • winget install: Tells Windows to install a new software package.
  • --id GitHub.cli: Specifies the exact package ID for GitHub CLI.

After running this command, GitHub CLI will be installed on our Windows system.

macOS:

We can use Homebrew to install GitHub CLI. Open our terminal and run:

brew install gh

Linux:

On Linux, we can use your package manager. For example, on Ubuntu, we can run:

sudo apt install gh

Authenticating with GitHub Account

After installing GitHub CLI, the next step is to authenticate it with our GitHub account.

Authentication Command:

gh auth login

Steps to Authenticate:

Run the Command:

Type gh auth login in the terminal and press Enter.

Choose Authentication Method:

  • We will be prompted to select an authentication method. The recommended option is to authenticate via a web browser.
  • If we select the browser method, GitHub CLI will open a link in our default browser, where we can log in to GitHub.

Complete Authentication:

After logging in, the browser will confirm that GitHub CLI is connected to our account.

We can verify the authentication status by running:

gh auth status

GitHub CLI is easy to navigate, and its command structure is intuitive.

Command Structure and Syntax

GitHub CLI commands follow a simple and straigntforward pattern:

gh [command] [subcommand] [flags]
  • Command: The main action you want to perform (e.g., repo, issue, pr).
  • Subcommand: A specific task within the command (e.g., create, list, view).
  • Flags: Optional parameters that modify the command's behavior (e.g., --title, --body).

Commonly Used Commands and Flags

Here are some common GitHub CLI commands:

  • Creating a repository: gh repo create
  • Listing issues: gh issue list
  • Creating a pull request: gh pr create
  • Viewing a repository's details: gh repo view

To see all available commands and options, we can always run:

gh help

Managing Repositories with GitHub CLI

Creating a new GitHub repository directly from the terminal can be done using the following command.

Creating and Cloning Repositories

To create a new repository, run:

gh repo create my-repo-name

To clone an existing repository, use:

gh repo clone owner/repo-name

Managing Branches and Pull Requests

GitHub CLI allows us to handle issues and pull requests (PRs) without leaving the terminal.

Commands:

Switching branches or creating pull requests is simple with GitHub CLI. To create a new branch:

git checkout -b new-branch-name

Then, to create a pull request

gh pr create --title "Your PR Title" --body "Description of your PR"

Pushing and Pulling Changes

Push our changes to GitHub with:

git push origin branch-name

And pull the latest changes with:

git pull

Working with GitHub Actions

GitHub CLI also supports GitHub Actions, allowing us to manage workflows directly from your terminal.

Triggering and Monitoring Workflows

You can manually trigger workflows using:

gh workflow run workflow-name

And monitor the status of workflows with:

gh run list

Managing Workflow Runs and Logs

To see detailed logs of a workflow run:

gh run view run-id --log

Cloning and Forking Repositories

Cloning and forking are essential tasks when working on projects from other repositories.

Commands:

To Clone a Repository:

gh repo clone <repository-name>

To Fork a Repository:

gh repo fork <repository-url>

Example:

Cloning a Repository:

gh repo clone example-repo

Forking a Repository:

gh repo fork https://github.com/username/repository-name

Interacting with GitHub Actions

GitHub Actions allows us to automate workflows directly from our repository. With GitHub CLI, we can manage and view these workflows on Windows.

Commands:

To List All Workflows:

gh workflow list

To View Workflow Runs:

gh run list

Working with Gists

Gists are an easy way to share small snippets of code or text files. Using GitHub CLI, we can create and manage gists.

Commands:

To Create a New Gist:

gh gist create <file>

To List Our Gists:

gh gist list

Updating GitHub CLI

To ensure that we are using the latest version of GitHub CLI with all the latest features and fixes, we can update it using winget.

Update Command:

winget upgrade --id GitHub.cli

where

  • winget upgrade: Checks for updates for the specified package.
  • --id GitHub.cli: Identifies the GitHub CLI package for the upgrade.

Managing Repositories with GitHub CLI

Managing repositories is one of the most common task which we perform with GitHub CLI.

Creating and Cloning Repositories

Create a new repository:

gh repo create my-new-repo

Clone an existing repository:

gh repo clone <repository-name>

Working with GitHub Actions

Using GitHub CLI we can also manage GitHub Actions, which are automated tasks we can run in response to certain events in your repository.

Triggering and Monitoring Workflows

Trigger a workflow manually:

gh workflow run <workflow-name>

Monitor workflow runs:

gh run list

Managing Workflow Runs and Logs

If we want to check the details of a specific workflow run, we can view logs directly from the CLI:

gh run view <run-id> --log

Integrating GitHub Actions Commands in CI/CD Pipelines

We can use GitHub CLI commands to enhance our Continuous Integration/Continuous Deployment (CI/CD) pipelines, ensuring smooth automation and better control over our workflows.

Advanced Features and Integrations

GitHub CLI is not only used for performing basic tasks. We can perform some advanced operation using GitHub CLI:

Managing Gists with GitHub CLI

Gists are a simple way to share snippets of code. We can create, list, and manage your Gists with commands like:

gh gist create my-code-snippet.py

Interacting with Releases and Tags

To manage releases and tags, GitHub CLI provides commands to create, list, and delete releases:

gh release create v1.0.0

Extending GitHub CLI with Custom Scripts and Aliases

We can write our own scripts and integrate them into GitHub CLI, or create aliases for commands you use frequently to save time. For example:

gh alias set prlist "pr list --state all"

Best Practices and Tips

Below some best practices are given:

Streamlining Workflow with Shortcuts and Aliases

Create aliases for our most-used commands to speed up your workflow. For example:

gh alias set co "pr checkout"

Automating Tasks Using Scripts

We can combine GitHub CLI commands in scripts to automate repetitive tasks, like automating the process of creating a branch, pushing changes, and opening a pull request.

Troubleshooting Common Issues

If we face any issues, we can troubleshoot by checking the command syntax, ensuring our GitHub CLI is up to date, or consulting the documentation using command:

gh help <command>

Next Article
Article Tags :

Similar Reads