How to Install GitHub CLI and Set up Authentication

What do you use when operating from a terminal against a repository on GitHub?

You are likely using a git program.

Common Git operations, such as clone, commit, push, pull, fetch, etc., can be handled by git programs like repositories on other servers.

However, git programs cannot manipulate GitHub-specific information. For example, a `git` program cannot retrieve and write a list of issues to a file.

You can use gh, GitHub’s CLI program to manipulate such GitHub-specific information. This article explains how to install gh and log in to GitHub.

TOC

Install gh

The method of installing gh depends on your operating system.

macOS

For macOS, you can install it via Homebrew. Run the following in a terminal.

brew install gh

You can check the installed version as follows. If it is displayed, the installation was successful.

gh --version
gh version 2.28.0 (2023-04-25)
https://github.com/cli/cli/releases/tag/v2.28.0

Windows

For Windows, you can probably install via brew by installing Homebrew. Still, it is better to use winget: execute the following at the PowerShell or command prompt.

winget install --id GitHub.cli

You will be prompted to confirm your agreement with the source terms. Type y to accept, initiating the installation process.

'msstore' ソースを使用するには、使用する前に次の契約を表示する必要があります。
Terms of Transaction: https://aka.ms/microsoft-store-terms-of-transaction
ソースが正常に機能するには、現在のマシンの 2 文字の地理的リージョンをバックエンド サービスに送信する必要があります (例: "US")。

すべてのソース契約条件に同意しますか?
[Y] はい  [N] いいえ: y
見つかりました GitHub CLI [GitHub.cli] バージョン 2.28.0
このアプリケーションは所有者からライセンス供与されます。
Microsoft はサードパーティのパッケージに対して責任を負わず、ライセンスも付与しません。
Downloading https://github.com/cli/cli/releases/download/v2.28.0/gh_2.28.0_windows_amd64.msi
  ██████████████████████████████  9.89 MB / 9.89 MB
インストーラーハッシュが正常に検証されました
パッケージのインストールを開始しています...
インストールが完了しました

When the installation is complete, the command prompt will wait for input; restart PowerShell (or the command prompt) to reflect the PATH change.

After restarting, check the version by typing as follows. If the version is displayed, the installation is successful.

gh --version
gh version 2.28.0 (2023-04-25)
https://github.com/cli/cli/releases/tag/v2.28.0

Other methods

The documentation linked from the official site explains other methods of installation. Therefore, if you wish to install by other methods, please refer to that document.

Login with gh

To log in to GitHub with gh, execute the following command.

gh auth login

You will be prompted for a login server. In this case, we will log in to GitHub.com. Select GitHub.com and press enter.

? What account do you want to log into?  [Use arrows to move, type to filter]
> GitHub.com
  GitHub Enterprise Server

A confirmation of the protocol to be used will appear. For example, the author uses HTTPS, so select HTTPS and press enter.

? What is your preferred protocol for Git operations?  [Use arrows to move, type to filter]
> HTTPS
  SSH

You will be prompted to confirm whether you want to authenticate to GitHub. Type y to proceed with authentication.

? Authenticate Git with your GitHub credentials? (Y/n) y

The GitHub CLI will prompt you to confirm whether you want to log in with a web browser or use an authentication token. The author chose the web browser.

? How would you like to authenticate GitHub CLI?  [Use arrows to move, type to filter]
> Login with a web browser
  Paste an authentication token

A one-time code will be displayed. Press Enter to launch your browser; if you are not logged in to GitHub with a browser, you will be taken to the GitHub login screen. After logging in, enter the one-time code when the next page appears.

Enter the one-time code for the GitHub CLI
Enter the one-time code for the GitHub CLI

Next, you will be prompted to confirm the operation you wish to authorize. Press “Authorize github” to allow it.

Review the permission.
Review the permission.

If successful, the completion screen will be displayed.

Completed
Completed

The shell also displays the following.

✓ Logged in as AccountName

It takes a few seconds after the completion screen appears in your browser.

Authored Books

Let's share this post !

Author of this article

Akira Hayashi (林 晃)のアバター Akira Hayashi (林 晃) Representative(代表), Software Engineer(ソフトウェアエンジニア)

アールケー開発代表。Appleプラットフォーム向けの開発を専門としているソフトウェアエンジニア。ソフトウェアの受託開発、技術書執筆、技術指導・セミナー講師。note, Medium, LinkedIn
-
Representative of RK Kaihatsu. Software Engineer Specializing in Development for the Apple Platform. Specializing in contract software development, technical writing, and serving as a tech workshop lecturer. note, Medium, LinkedIn

TOC