2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ghコマンドを使えるようにする手順

2
Posted at

GitHub CLI の gh コマンドを使えるようにしたので、インストールから認証確認までの手順をまとめます。

gh を入れると、ターミナルから GitHub のリポジトリ、Issue、Pull Request などを操作できます。

前提

この記事では、macOS + Homebrew の環境を中心に書きます。

OS: macOS
Package manager: Homebrew
対象コマンド: gh

Windows や Linux の場合も公式のインストール手順があります。

ghをインストールする

Homebrew を使っている場合は、次のコマンドでインストールできます。

brew install gh

インストールが終わったら、gh コマンドが見つかるか確認します。

command -v gh

次のようにパスが表示されればOKです。

/opt/homebrew/bin/gh

Intel Mac や環境によっては /usr/local/bin/gh になることもあります。

バージョンを確認する

次に、バージョンを確認します。

gh --version

例:

gh version 2.93.0 (2026-05-27)
https://github.com/cli/cli/releases/tag/v2.93.0

ここまで表示されれば、gh のインストール自体はできています。

GitHubにログインする

gh を GitHub アカウントで使うには認証が必要です。

gh auth login

実行すると、対話形式でいくつか聞かれます。

代表的には次のような流れです。

GitHub.com を選ぶ
HTTPS または SSH を選ぶ
ブラウザ認証を選ぶ
表示されたワンタイムコードを GitHub の画面で入力する

迷ったら、最初は次の組み合わせで問題ありません。

Host: GitHub.com
Git protocol: HTTPS
Authentication: browser

ブラウザ認証では、ターミナルにワンタイムコードが表示されます。

First copy your one-time code: XXXX-XXXX
Press Enter to open https://github.com/login/device in your browser...

ブラウザで GitHub にログインし、表示されたコードを入力して承認します。

認証が完了すると、次のような表示になります。

Authentication complete.
Configured git protocol
Logged in as your-account

認証状態を確認する

ログインできたか確認します。

gh auth status

成功していれば、次のように表示されます。

github.com
  ✓ Logged in to github.com account your-account
  - Active account: true
  - Git operations protocol: https

Token scopes も表示されます。

  - Token scopes: 'gist', 'read:org', 'repo', 'workflow'

表示される scope は、ログイン時の設定や追加した権限によって変わります。

リポジトリ情報を取得してみる

GitHub リポジトリのディレクトリ内で、次のコマンドを実行します。

gh repo view

JSON で確認したい場合は、次のようにできます。

gh repo view --json nameWithOwner,url

例:

{
  "nameWithOwner": "your-account/your-repository",
  "url": "https://github.com/your-account/your-repository"
}

ここまで通れば、gh コマンドは使える状態です。

よくあるエラー

command not found: gh

gh がインストールされていないか、PATH が通っていません。

まずは次を確認します。

command -v gh
brew list gh

brew list gh で見つからなければ、インストールします。

brew install gh

You are not logged into any GitHub hosts

まだ GitHub にログインしていません。

gh auth login

でログインします。

The token in default is invalid

保存されている認証情報が無効になっている可能性があります。

再ログインします。

gh auth login -h github.com

古い認証情報を消したい場合は、次のコマンドを使います。

gh auth logout -h github.com

まとめ

gh コマンドを使えるようにする流れは、次の4ステップです。

brew install gh
gh --version
gh auth login
gh auth status

認証まで済ませておくと、Pull Request の確認や作成、Issue の操作、リポジトリ情報の取得などをターミナルからできるようになります。

参考

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?