iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🚀

GitHub CLI is Incredibly Useful

に公開

Introduction

Hello there. I'm @caru_ini.
Do you know about GitHub's official command-line tool, "GitHub CLI"? While many developers likely use GitHub's web interface, you can streamline your GitHub operations by utilizing GitHub CLI.
I've been using it personally, and it's so convenient that I just had to share its benefits with everyone! That's why I decided to write this article.

In this article, I will introduce everything from the basic usage to advanced techniques of GitHub CLI and show you how to dramatically improve your development productivity.

Why GitHub CLI?

By using GitHub CLI, for example, you can do the following!

  1. Save work time by performing GitHub operations directly from the command line.
  2. Reduce mistakes by executing complex operations with simple commands.
  3. Manage multiple accounts more easily.

Now, let's take a look at the details and usage of GitHub CLI.

What is GitHub CLI?

GitHub CLI is a tool for performing GitHub operations from the command line. It allows you to perform various GitHub actions, such as Git integration and managing Issues or Pull Requests, directly from the terminal.

https://docs.github.com/en/github-cli/github-cli/about-github-cli

Installation

Here are the installation methods for each OS.

macOS

brew install gh

Linux / BSD

Manually add the repository to apt and install.
(It's a bit long...)

(type -p wget >/dev/null || (sudo apt update && sudo apt-get install wget -y)) \
 && sudo mkdir -p -m 755 /etc/apt/keyrings \
 && wget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \
 && sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \
 && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
 && sudo apt update \
 && sudo apt install gh -y

Windows

Install using winget (package manager).
I believe it is installed by default on recent versions of Windows 10/11.

winget install --id GitHub.cli

Login

Once the installation is complete, let's first log in with your GitHub account.

gh auth login -w -p https 

Since a code will be displayed in the format XXXX-XXXX, enter that code in your browser to authenticate.

gh auth status

If it is displayed as follows, you're all set.

 Logged in to github.com account <account name> (/home/user/.config/gh/hosts.yml)
- Active account: true
- Git operations protocol: https
- Token: gho_************************************
- Token scopes: 'gist', 'read:org', 'repo', 'workflow'

Switching Accounts

If you have multiple accounts, you can add them and switch using the following command:

gh auth switch

The output will look like this:

 Switched active account for github.com to <account-name>

Basic Usage

Creating a Repository

To create a new repository, use the following command:

gh repo create <repository-name>

If you omit the repository name, you can create the repository interactively.

Frequently Used Options

  • -c, --clone: Clone the repository immediately after creation
  • --private: Create a private repository
  • -p, --template: Specify a repository template

Cloning a Repository

To clone an existing repository, use the following command:

gh repo clone <repository-name / URL> <local-path>

This command is basically the same as git clone, but it's convenient because it clones with the remote already configured. If you omit the local path, a directory will be created using the repository name.
While it looks just like git clone, for repositories on GitHub, you can also write it like this!

  • (username or Org name)/repository-name
gh repo clone caru-ini/test-repo
  • Repository name only (for your own repositories)
gh repo clone test-repo

Overriding Git Commands

You can pass options after -- to the underlying Git command.

gh repo clone <repository-name / URL> <local-path> -- --depth=1

Setting up Git

By setting up Git via GitHub CLI, you can easily start using Git. Usually, you would set this up with SSH keys, but this is all you need, which is very convenient.

gh auth setup-git

Running this command adds the following settings to your global .gitconfig:

[credential "https://github.com"]
        helper =
        helper = !/usr/bin/gh auth git-credential
[credential "https://gist.github.com"]
        helper =
        helper = !/usr/bin/gh auth git-credential

If you haven't set your username and email address in Git yet, be sure to add them.

Opening the Web Page of the Repository

If a GitHub repository is set as a Git remote, you can open the repository's web page with the following command:

gh browse

Personally, I find it tedious to open GitHub and then search for the repository, so I use this command frequently!

Creating an Issue

To create a new issue, use the following command:

gh issue create

If the required options are not specified, you can create the issue interactively.

Frequently Used Options

  • -t, --title: Title
  • -b, --body: Body
  • -a, --assignee: Assignee
  • -l, --label: Label
  • -w, --web: Open the issue in a browser

Usage Example

gh issue create -t "Bug Report" -b "This is a bug report" -a "caru-ini" -l "bug" -l "critical"

Listing Issues

To display a list of issues, use the following command:

gh issue list

This command displays the list of issues with color coding, making it very easy to read.

Frequently Used Options

  • -s, --state: Status (open | closed | all)
  • -A, --author: Author
  • -a, --assignee: Assignee
  • -l, --label: Label

Creating a Pull Request

To create a pull request, first switch to the branch you want to submit and execute the following command:

gh pr create

If the required options are not specified, you can create the pull request interactively.

Frequently Used Options

  • -t, --title: Title
  • -b, --body: Body
  • -a, --assignee: Assignee
  • -l, --label: Label
  • -d, --draft: Create a draft

Usage Example

gh pr create -t "Fix bug" -b "This is a bug fix" -a "caru-ini" -l "bug"

Listing Pull Requests

To display a list of pull requests, use the following command:

gh pr list

Checking Out a Pull Request

To check out a pull request's branch, execute the following command:

gh pr checkout <Request Number>

Using Extensions

I will also show you how to extend the functionality of GitHub CLI.

GitHub Copilot

GitHub Copilot can also be used with GitHub CLI. You can install it with the following command:

gh extension install github/gh-copilot

After installation, the gh copilot command will be available.

Explaining Commands

gh copilot explain <command>

This command provides an explanation of the specified command. If no command is specified, you can enter the command you want explained interactively.

Suggesting Commands

gh copilot suggest <what you want to do>

When you enter what you want to do, it suggests the command to execute. You will first be asked for the type of command, so select the appropriate one.

  • generic shell: General shell commands
  • gh: GitHub CLI commands
  • git: Git commands

Summary

Thank you for reading until the end!
In this article, I introduced the basic usage of GitHub CLI and the GitHub Copilot extension for GitHub CLI. The ability to operate not just Git but also GitHub from the command line makes it a tool that will truly resonate with those who need it.
The features introduced here are only a part of what GitHub CLI offers. Please refer to the official documentation and explore even more useful features!

References

Promotion

I am studying Computer Science at university while casually developing and participating in hackathons using front-end technologies such as React, TypeScript, and Next.js!
I don't have many friends, so I would be very happy if you could follow me!

https://x.com/caru_ini

GitHubで編集を提案

Discussion