#GitHub CLIとは

GitHub CLI は、コンピューターのコマンド ラインから GitHub を使用するためのオープン ソース ツールです。 コマンドラインから作業しているときは、GitHub CLI を使用して時間を節約し、コンテキストの切り替えを回避できます。

GitHub CLIについて より引用。

私はNeovimを使って開発しているため、ターミナルで作業しています。 そんなときにGitHub CLIを用いてGitHubの操作ができると非常に便利です。

#本記事での実行環境

  • macOS Sequoia 15.6.1
  • gh version 2.78.0 (2025-08-21)

#インストール

Mac OSの場合は、Homebrewからインストールできます。

brew install gh

その後、GitHubアカウントの認証を行います。

gh auth login

使用するエディタを指定するには、gh config set editor <editor>を使用します。私の場合はvimです。

その他の環境はこちら を参照してください。

#使い方

使い方はGitHub CLI manual に書いてあります。

この記事では、私がよく使うコマンドを紹介しようと思います。 前提として、GitHub は個人開発で主に使用しています。

##リポジトリの作成

主要な使い方は、リポジトリを新しく作る or すでに作成済みのリポジトリを Clone してくる です。

gh repo create
# ? What would you like to do?  [Use arrows to move, type to filter]
# > Create a new repository on github.com from scratch
# Create a new repository on github.com from a template repository
# Push an existing local repository to github.com

はじめに新しい GitHub にリポジトリを新規作成するか、ローカルのポジトリを push するか聞かれます。割とどっちも使います。

そのほかにも色々聞かれるので答えます。

gh repo create
# ? What would you like to do? Create a new repository on github.com from scratch
# ? Repository name test
# ? Repository owner xxx
# ? Description This is a test repo
# ? Visibility Public
# ? Would you like to add a README file? No
# ? Would you like to add a .gitignore? No
# ? Would you like to add a license? No
# ? This will create "test" as a public repository on github.com. Continue? Yes
# ✓ Created repository xxx/test on github.com
#   https://github.com/xxx/test
# ? Clone the new repository locally? Yes

コマンドが終了するとローカルにディレクトリが作られています。

##リポジトリをブラウザで開く

CLI を使うとか言っておきながら、めちゃくちゃ使います。

gh browse

##Issueを操作する

Issueを作成するには以下のコマンドを使用します。

gh issue create

オプションを何も指定しないと、 Title や Body がインタラクティブに尋ねられます。

gh issue create

# Creating issue in xxx/test

# ? Title (required) Test issue
# ? Choose a template Open a blank issue
# ? Body <Received> エディタを開いて body を編集できます
# ? What's next? Submit
# https://github.com/xxx/test/issues/1

主要なオプションは以下の通りです:

  • -t--title: タイトル
  • -b--body: 内容
  • -l--label: ラベルの追加
  • -a--assignee: 内容
    • @me (自分自身) や @copilot などが使用できます。

e.g.

gh issue create --title "Sample issue" --body "This is a sample issue" --label enhancement --assignee @me

Creating issue in xxx/test

https://github.com/xxx/test/issues/2

Issue の一覧は以下のようにしてみることができます。

gh issue list
# Showing 2 of 2 open issues in xxx/test

# ID  TITLE         LABELS       UPDATED
# #2  Sample issue  enhancement  about 2 minutes ago
# #1  Test issue                 about 8 minutes ago

Issue の詳細は以下のようにして見ます。

gh issue view 2 # 数字はIssue の番号
# Sample issue xxx/test#2
# Open • xxx opened about 2 minutes ago • 0 comments
# Assignees: xxx 
# Labels: enhancement


# This is a sample issue


# View this issue on GitHub: https://github.com/xxx/test/issues/2

# (END)

##PR の操作

特によく使います。

まずは Issue の作業のためのブランチを作成します。

gh issue develop 1 --name test --checkout
# github.com/xxx/test/tree/test
# From github.com:xxx/test
# * [new branch]      test       -> origin/test

それぞれのオプションの意味は以下の通りです。

  • --name: ブランチの名前
  • --checkout: ブランチの作成後 checkout する

作成したブランチで実際に作業を行います。

次に PR を出します。

gh pr create

# Creating pull request for test into main in xxx/test

# ? Title (required) Test PR
# ? Choose a template Open a blank pull request
# ? Body <Received> # エディタで body を編集できます
# ? What's next? Submit
# https://github.com/xxx/test/pull/3

PR への Reviewer や Assignees は以下のコマンドで編集できます。 私の環境では Reviewer はうまくアサインできなかった。 (私しかリポジトリにいないから?)

gh pr edit
# ? What would you like to edit? Body, Reviewers, Assignees
# ? Body <Received>
# ? Assignees xxx (xxx)
# ? Submit? Yes
# https://github.com/xxx/test/pull/3

PR をマージします。 このときにGithub とローカル上のブランチの削除できます。

gh pr merge
# Merging pull request xxx/test#3 (Test PR)
# ? What merge method would you like to use? Create a merge commit
# ? Delete the branch locally and on GitHub? Yes
# ? What's next? Submit
# ✓ Merged pull request xxx/test#3 (Test PR)
# remote: Enumerating objects: 1, done.
# remote: Counting objects: 100% (1/1), done.
# remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
# Unpacking objects: 100% (1/1), 879 bytes | 879.00 KiB/s, done.
# From github.com: xxx/test
#  * branch            main       -> FETCH_HEAD
#    68aa932..14f4a9e  main       -> origin/main
# Updating 68aa932..14f4a9e
# Fast-forward
#  hoge.txt | 1 +
#  1 file changed, 1 insertion(+)
#  create mode 100644 hoge.txt
# ✓ Deleted local branch test and switched to branch main
# ✓ Deleted remote branch test

##GitHub Actions のワークフローの操作

GHA のワークフローの操作もできますが、私はたまにしか使わないです。

gh run --help
# List, view, and watch recent workflow runs from GitHub Actions.
# 
# USAGE
#   gh run <command> [flags]
# 
# AVAILABLE COMMANDS
#   cancel:        Cancel a workflow run
#   delete:        Delete a workflow run
#   download:      Download artifacts generated by a workflow run
#   list:          List recent workflow runs
#   rerun:         Rerun a run
#   view:          View a summary of a workflow run
#   watch:         Watch a run until it completes, showing its progress
# 
# FLAGS
#   -R, --repo [HOST/]OWNER/REPO   Select another repository using the [HOST/]OWNER/REPO format
# 
# INHERITED FLAGS
#   --help   Show help for command
# 
# LEARN MORE
#   Use `gh <command> <subcommand> --help` for more information about a command.
#   Read the manual at https://cli.github.com/manual
#   Learn about exit codes using `gh help exit-codes`
#   Learn about accessibility experiences using `gh help accessibility`
#