GitHub CLI(gh)
この章では、GitHub公式CLIツール「gh」のインストール、主要コマンド、エイリアスについて学びます。
GitHub CLIとは
Section titled “GitHub CLIとは”**GitHub CLI(gh)**は、GitHubの操作をターミナルから行える公式コマンドラインツールです。Issue、PR、リリースなどの操作をコマンドラインで完結できます。
| 特徴 | 説明 |
|---|---|
| 公式ツール | GitHub公式サポート |
| 統合認証 | ブラウザ認証でセットアップ簡単 |
| 高機能 | REST/GraphQL API直接アクセス |
| 拡張性 | エイリアスと拡張機能 |
インストール
Section titled “インストール”# Homebrewbrew install gh
# MacPortssudo port install ghWindows
Section titled “Windows”# wingetwinget install --id GitHub.cli
# Chocolateychoco install gh
# Scoopscoop install gh# Ubuntu/Debiantype -p curl >/dev/null || sudo apt install curl -ycurl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpgecho "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/nullsudo apt updatesudo apt install gh
# Fedorasudo dnf install gh
# Arch Linuxsudo pacman -S github-cli# 認証(ブラウザが開く)gh auth login
# 認証状況の確認gh auth status
# 複数アカウントの管理gh auth login --hostname github.company.comgh auth switch主要コマンド
Section titled “主要コマンド”リポジトリ操作
Section titled “リポジトリ操作”# リポジトリのクローンgh repo clone owner/repo
# リポジトリの作成gh repo create my-repo --publicgh repo create my-repo --privategh repo create my-repo --template owner/template
# リポジトリの表示gh repo viewgh repo view owner/repo --web # ブラウザで開く
# リポジトリの一覧gh repo listgh repo list owner --limit 20
# リポジトリのフォークgh repo fork owner/repogh repo fork owner/repo --cloneIssue操作
Section titled “Issue操作”# Issue一覧gh issue listgh issue list --state opengh issue list --assignee @megh issue list --label bug
# Issue作成gh issue creategh issue create --title "Bug report" --body "Description"gh issue create --title "Bug" --label bug --assignee @me
# Issue表示gh issue view 123gh issue view 123 --web
# Issue編集gh issue edit 123 --title "New title"gh issue edit 123 --add-label enhancementgh issue edit 123 --add-assignee user1
# Issueのクローズ/再開gh issue close 123gh issue reopen 123
# Issueへのコメントgh issue comment 123 --body "Comment text"Pull Request操作
Section titled “Pull Request操作”# PR一覧gh pr listgh pr list --state mergedgh pr list --author @megh pr list --draft
# PR作成gh pr creategh pr create --title "Feature" --body "Description"gh pr create --fill # コミットメッセージから自動入力gh pr create --draftgh pr create --base develop --head feature/auth
# PR表示gh pr view 123gh pr view 123 --webgh pr view --comments
# PRのチェックアウトgh pr checkout 123
# PRのマージgh pr merge 123gh pr merge 123 --squashgh pr merge 123 --rebasegh pr merge 123 --auto --squash # 自動マージ有効化
# PRのレビューgh pr review 123 --approvegh pr review 123 --request-changes --body "Please fix..."gh pr review 123 --comment --body "Looks good!"
# PRの差分gh pr diff 123ワークフロー操作
Section titled “ワークフロー操作”# ワークフロー一覧gh workflow list
# ワークフロー実行履歴gh run list
# 特定のワークフロー実行gh workflow run deploy.ymlgh workflow run build.yml --ref feature-branchgh workflow run deploy.yml -f environment=production
# 実行結果の確認gh run viewgh run view 12345gh run view 12345 --loggh run watch 12345 # リアルタイム監視
# 実行の再実行gh run rerun 12345gh run rerun 12345 --failed # 失敗したジョブのみリリース操作
Section titled “リリース操作”# リリース一覧gh release list
# リリース作成gh release create v1.0.0gh release create v1.0.0 --title "Version 1.0.0" --notes "Release notes"gh release create v1.0.0 --generate-notesgh release create v1.0.0 ./dist/app-linux ./dist/app-macos
# リリース表示gh release view v1.0.0
# アセットのダウンロードgh release download v1.0.0gh release download v1.0.0 --pattern "*.zip"
# リリース削除gh release delete v1.0.0Gist操作
Section titled “Gist操作”# Gist一覧gh gist list
# Gist作成gh gist create script.pygh gist create script.py --desc "Utility script"gh gist create file1.js file2.js
# Gist表示gh gist view <gist-id>
# Gist編集gh gist edit <gist-id>
# Gist削除gh gist delete <gist-id>Codespace操作
Section titled “Codespace操作”# Codespace一覧gh codespace list
# Codespace作成gh codespace create --repo owner/repogh codespace create --repo owner/repo --machine largePremiumLinux
# Codespaceに接続gh codespace ssh -c <codespace-name>gh codespace code -c <codespace-name>
# Codespace停止/削除gh codespace stop -c <codespace-name>gh codespace delete -c <codespace-name>API直接アクセス
Section titled “API直接アクセス”REST API
Section titled “REST API”# GET リクエストgh api /repos/owner/repo
# POST リクエストgh api /repos/owner/repo/issues --method POST \ -f title="Issue title" \ -f body="Issue body"
# ページネーションgh api /repos/owner/repo/issues --paginate
# JQでフィルタリングgh api /repos/owner/repo/issues --jq '.[].title'gh api /repos/owner/repo/contributors --jq '.[].login'
# 生のJSON出力gh api /user --jq '.'GraphQL API
Section titled “GraphQL API”# GraphQLクエリgh api graphql -f query=' query { viewer { login repositories(first: 10) { nodes { name stargazerCount } } } }'
# 変数を使ったクエリgh api graphql -f query=' query($owner: String!, $repo: String!) { repository(owner: $owner, name: $repo) { issues(first: 10, states: OPEN) { nodes { title number } } } }' -f owner=octocat -f repo=Hello-Worldエイリアスの設定
Section titled “エイリアスの設定”# エイリアスの作成gh alias set co 'pr checkout'gh alias set pv 'pr view --web'gh alias set iv 'issue view --web'
# 引数付きエイリアスgh alias set clone 'repo clone "$1"'gh alias set myissues 'issue list --assignee @me'
# 複合コマンドgh alias set pr-files 'pr view --json files --jq ".files[].path"'
# エイリアス一覧gh alias list
# エイリアス削除gh alias delete co便利なエイリアス例
Section titled “便利なエイリアス例”# ~/.config/gh/config.yml に記載される
# PR関連gh alias set prc 'pr create --fill'gh alias set prm 'pr merge --squash --delete-branch'gh alias set prs 'pr status'
# Issue関連gh alias set ic 'issue create'gh alias set il 'issue list --assignee @me'
# ワークフロー関連gh alias set runs 'run list --limit 5'gh alias set watch 'run watch'
# ブラウザで開くgh alias set web 'repo view --web'gh alias set prweb 'pr view --web'拡張機能のインストール
Section titled “拡張機能のインストール”# 拡張機能の検索gh extension search
# インストールgh extension install github/gh-copilotgh extension install dlvhdr/gh-dashgh extension install mislav/gh-branch
# 一覧gh extension list
# 更新gh extension upgrade --all
# 削除gh extension remove gh-copilot人気の拡張機能
Section titled “人気の拡張機能”| 拡張機能 | 説明 |
|---|---|
github/gh-copilot | Copilot in CLI |
dlvhdr/gh-dash | ダッシュボード |
mislav/gh-branch | ブランチ管理 |
seachicken/gh-poi | 古いブランチ削除 |
vilmibm/gh-screensaver | スクリーンセーバー |
中級者向けTips
Section titled “中級者向けTips”設定ファイル
Section titled “設定ファイル”git_protocol: ssheditor: vimprompt: enabledpager: less
aliases: co: pr checkout pv: pr view --web
hosts: github.com: user: your-username oauth_token: ghp_xxxxシェルの補完設定
Section titled “シェルの補完設定”# Basheval "$(gh completion -s bash)"
# Zsheval "$(gh completion -s zsh)"
# Fishgh completion -s fish | source複数アカウントの切り替え
Section titled “複数アカウントの切り替え”# Enterprise追加gh auth login --hostname github.company.com
# アカウント切り替えgh auth switch
# 特定ホストを指定gh repo list --hostname github.company.com自動化スクリプト例
Section titled “自動化スクリプト例”#!/bin/bash# 全PRのステータス確認
for repo in $(gh repo list --json name --jq '.[].name'); do echo "=== $repo ===" gh pr list --repo "owner/$repo" --state opendone#!/bin/bash# 古いブランチの削除
gh api /repos/owner/repo/branches --paginate --jq ' .[] | select(.name != "main" and .name != "develop") | .name' | while read branch; do echo "Delete: $branch" gh api -X DELETE "/repos/owner/repo/git/refs/heads/$branch"done| カテゴリ | 主要コマンド |
|---|---|
| リポジトリ | gh repo |
| Issue | gh issue |
| PR | gh pr |
| ワークフロー | gh run, gh workflow |
| リリース | gh release |
| API | gh api |
GitHub CLIのベストプラクティス
Section titled “GitHub CLIのベストプラクティス”- エイリアス活用: よく使うコマンドを短縮
- シェル補完: Tab補完で効率アップ
- 拡張機能: 必要に応じて機能追加
- API直接アクセス: 複雑な操作も可能
- スクリプト化: 定型作業を自動化
よく使うコマンドチートシート
Section titled “よく使うコマンドチートシート”# 日常的なワークフローgh pr create --fill # PR作成gh pr checkout 123 # PRをチェックアウトgh pr merge --squash # マージgh issue create # Issue作成gh run watch # CI監視
# 確認系gh pr status # 自分のPR状況gh issue list --assignee @me # 自分のIssuegh repo view --web # ブラウザで開く次の章では、GitHub APIについて学びます。
Q1. GitHub CLIの認証を開始するコマンドはどれですか?
Q2. GitHub CLIでPRを作成してコミットメッセージから自動入力するオプションはどれですか?
Q3. GitHub CLIでREST APIを直接呼び出す方法はどれですか?