3
0

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コマンドを一通り触る

Posted at

はじめに

Claude CodeやDevinが開発に加わってきてい色々と便利になった。githubと連携して勝手にプルリク出してくれたり、レビューのコメントに対応してくれたり、マジ便利。
そんな便利なAI達が裏でどんなコマンドを叩いているか位は理解できておかないとAIに取り残されてしまう。
ghコマンドの存在は知っていたが、直接叩く機会がなかったので改めて学んでみた。

ghコマンドは、github cli公式にある通り、プルリク作ったりリリース作ったり色々できる。学ぶと言う以上は、公式の1ページ目にある例ぐらいは一通りやる。

公式の1ページ目にある例

  • リポジトリの表示、作成、複製、フォーク
  • Issue と pull request の作成、クローズ、編集、一覧表示
  • プルリクエストのレビュー、diff、マージ
  • ワークフローの実行、表示、一覧表示
  • リリースの作成、一覧表示、表示、削除
  • gist の作成、編集、一覧表示、表示、削除
  • codespace の一覧表示、作成、削除、接続
  • GitHub API から情報を取得します

諸注意: 一通りやると書いたが、めんどくさくなったのでcodespaceとapiはやらない。

まとめ

一通り試したおかげで内容はともかくリポジトリの作成から初回リリースまでコマンドで実行できるようになってしまった。
公式マニュアルを見る限り他にもextensionとかprojectとかあるし、コマンドで出たエラー見る限りGraphQL使ってるっぽい。
仕事的にはAIにがりがり書いてもらう所になるんだろうけど、何かしらAIが困ったことになったら役に立たせたい。

学び

インストール & 初期設定

公式のインストール手順に則りさらっとインストール。

ヘルプもちゃんとでる

gh -h
Work seamlessly with GitHub from the command line.

USAGE
  gh <command> <subcommand> [flags]

CORE COMMANDS
  auth:          Authenticate gh and git with GitHub
  browse:        Open repositories, issues, pull requests, and more in the browser

Githubでトークン発行して、gh auth loginで認証する。
一通り試すので基本全部権限付与する。危険なトークンになるので厳重に管理する。

gh auth statusでログイン済みになっていればOK。

$ gh auth status
github.com
  ✓ Logged in to github.com account miyatama (GITHUB_TOKEN)
  - Active account: true
  - Git operations protocol: https
  - Token: ghp_************************************
  - Token scopes: ...

リポジトリの表示、作成、複製、フォーク

repoのマニュアルを見ながら表示・作成・複製・フォークをやってみる。

まずはリポジトリの表示(gh repo list)。キレイに出てくれる。パイプで整形したいところだが、Windows環境なのが悔やまれる。

$ gh repo list

Showing 30 of 41 repositories in @miyatama

NAME                                         DESCRIPTION                                                      INFO          UPDATED
miyatama/learn_rust                                                                                           public        about 1 month ago
miyatama/rust_console_project_template       コンソールアプリ作成用テンプレート                               public        about 4 months ago
miyatama/learn-fsharp                                                                                         public        about 6 months ago
miyatama/about_android                       android learning repository                                      public        about 9 months ago
miyatama/github-actions-iot-flow1                                                                             public        about 10 months ago
miyatama/about_xr                            xr関連の色々なものを置く場所                                     public        about 10 months ago

続いて作成

gh repo create miyatama/learn-gh-cli \
  --public \
  --add-readme \
  -d "ghコマンド練習用のリポジトリ"
✓ Created repository miyatama/learn-gh-cli on github.com
  https://github.com/miyatama/learn-gh-cli

あたりまえだけどちゃんとリポジトリ作ってくれる

miyatama/learn-gh-cli

01.png

複製 is 何?と思ったらはgit cloneと同じらしい。一応やる。

gh repo clone miyatama/learn-gh-cli
Cloning into 'learn-gh-cli'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (3/3), done.

最後にfork。GitHub の Public リポジトリを Private に Fork するにforkテスト用リポジトリがあるので、一礼して利用させていただく。

git clone https://github.com/hatappo/fork-test
gh repo fork
? Would you like to add a remote for the fork? Yes
✓ Renamed remote origin to upstreamr the fork? (y/N) y
✓ Added remote origin

あたりまえだけどちゃんとforkできる。

miyatama/fork-test

Issue と pull request の作成、クローズ、編集、一覧表示、プルクエストのレビュー、diff、マージ

Issueの作成と一覧

gh issue create \
  --assignee "@me" \
  --title "ghコマンドのスクリプト追加してほしい" \
  --label "bug" \
  --body "リポジトリの作成とかは載せたい" \
  --repo miyatama/learn-gh-cli

Creating issue in miyatama/learn-gh-cli

https://github.com/miyatama/learn-gh-cli/issues/1

こんな感じ。プロジェクトの指定とかCopilot指定とかもできる。

02.png

一覧表示はjsonパースが便利

gh issue list \
  --repo miyatama/learn-gh-cli \
  --json "id,title,labels,updatedAt" \
  --assignee "@me"
[
  {
    "id": "I_kwDOPZniIs7EmOsl",
    "labels": [
      {
        "id": "LA_kwDOPZniIs8AAAACHI6hrg",
        "name": "bug",
        "description": "Something isn't working",
        "color": "d73a4a"
      }
    ],
    "title": "ghコマンドのスクリプト追加してほしい",
    "updatedAt": "2025-08-06T23:15:33Z"
  }
]

PRの作成

# 適当にブランチを切って修正をプッシュする
git checkout -b feature/add-repositories-sample
mkdir scripts
cat << EOF > ./scripts/show-repositories.sh
gh repo list
EOF
git add ./*
git commit -m "リポジトリのリスト出すやつ"
git push origin feature/add-repositories-sample

# PR用ファイルを作る
cat << EOF > /path/to/pr_body.md
# やってること

リポジトリのリスト出すスクリプトの追加
EOF

gh pr create \
  --assignee "@me" \
  --repo miyatama/learn-gh-cli \
  --base main \
  --head feature/add-repositories-sample \
  --title "リポジトリのリスト出す対応" \
  --body-file .\pr_body.md

Creating pull request for feature/add-repositories-sample into main in miyatama/learn-gh-cli

https://github.com/miyatama/learn-gh-cli/pull/2

ちゃんとPRができるし、マージ先ブランチ指定もできてる

03.png

一覧で確認して編集してみる。

gh pr list \
  -s open \
  --repo miyatama/learn-gh-cli \
  --json "id,number,title,state,url,baseRefName,headRefName,reviews"
[
  {
    "baseRefName": "main",
    "headRefName": "feature/add-repositories-sample",
    "id": "PR_kwDOPZniIs6i6oxb",
    "number": 2,
    "reviews": [],
    "state": "OPEN",
    "title": "リポジトリのリスト出す対応",
    "url": "https://github.com/miyatama/learn-gh-cli/pull/2"
  }
]
gh pr edit 2 \
  --repo miyatama/learn-gh-cli \
  --add-label bug
https://github.com/miyatama/learn-gh-cli/pull/2

ラベルが追加される

04.png

今度はクローズ -> 再オープン -> レビュー -> マージを連続でやってみる。

gh pr close 2 \
  --repo miyatama/learn-gh-cli \
  --comment "何となくのクローズ"
✓ Closed pull request miyatama/learn-gh-cli#2 (リポジトリのリスト出す対応)
gh pr reopen 2 \
  --repo miyatama/learn-gh-cli \
  --comment "必要に迫られた再オープン"
✓ Reopened pull request miyatama/learn-gh-cli#2 (リポジトリのリスト出す対応)
gh pr review 2 \
  --repo miyatama/learn-gh-cli \
  --approve \
  --body "LGTM!LGTM!LGTM!"
failed to create review: GraphQL: Can not approve your own pull request (addPullRequestReview)
gh pr merge 2 \
  --repo miyatama/learn-gh-cli \
  --merge \
  --body "ノールックマージ"
✓ Merged pull request miyatama/learn-gh-cli#2 (リポジトリのリスト出す対応)

自分で作ったPRなのでレビューは失敗するが、クローズとかマージとかは成功する。

05.png

ワークフローの実行、表示、一覧表示

準備

適当にビルドできるプロジェクトを追加する

mkdir rust-project
cd rust-project
cargo new sample-app

適当にワークフローを構築

merge-to-develop.yml
name: Merge to develop workflow

on:
  push:
    branches:
      - develop
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: least pull request
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh pr list --repo miyatama/learn-gh-cli --state open

ワークフローの表示

# 一覧
gh workflow list --repo miyatama/learn-gh-cli --json "id,name,path,state"
[
  {
    "id": 180418444,
    "name": ".github/workflows/merge-to-develop.yml",
    "path": ".github/workflows/merge-to-develop.yml",
    "state": "active"
  }
]

# 個別
gh workflow view 180418444 --repo miyatama/learn-gh-cli
.github/workflows/merge-to-develop.yml - merge-to-develop.yml
ID: 180418444

Total runs 2
Recent runs
   TITLE                                                      WORKFLOW                                BRANCH                              EVENT  ID
X  Merge pull request #4 from miyatama/feature/invalid-wo...  .github/workflows/merge-to-develop.yml  develop                             push   16869342743
X  workflowのディレクトリ名ミスってるね                       .github/workflows/merge-to-develop.yml  feature/invalid-workflow-directory  push   16869320053

ワークフローの実行

手動実行はデフォルトブランチに取り込む必要があるので、developをmainに取り込んで叩く。

$ gh workflow run 180418444 --repo miyatama/learn-gh-cli
✓ Created workflow_dispatch event for merge-to-develop.yml at main

To see runs for this workflow, try: gh run list --workflow="merge-to-develop.yml"

Pull Requestのマージではなくちゃんと手動
で実行されてる。

06.png

リリースの作成、一覧表示、表示、削除

リリースの作成

まずはタグありきなのでmainにタグ打ってローカルでリリースビルド作る。

git checkout main
git fetch
git pull origin main
git tag 1.0.0
git push origin 1.0.0

リリース作成

gh release create 1.0.0 .\rust-project\sample-app\target\release/sample-app.exe \
  --title "初リリースバージョン" \
  --notes-file ../release-notes.md \
  --repo miyatama/learn-gh-cli
https://github.com/miyatama/learn-gh-cli/releases/tag/1.0.0

07.png

表示

gh release list --repo miyatama/learn-gh-cli --json "tagName,name,createdAt,publishedAt"
[
  {
    "createdAt": "2025-08-11T02:52:12Z",
    "name": "初リリースバージョン",
    "publishedAt": "2025-08-11T04:26:27Z",
    "tagName": "1.0.0"
  }
]

gh release view 1.0.0 --repo miyatama/learn-gh-cli --json "tagName,url,zipballUrl"
{
  "tagName": "1.0.0",
  "url": "https://github.com/miyatama/learn-gh-cli/releases/tag/1.0.0",
  "zipballUrl": "https://api.github.com/repos/miyatama/learn-gh-cli/zipball/1.0.0"
}

削除

適当にタグをきって削除してみる

git checkout main
git fetch
git pull origin main
git tag 1.0.1
git push origin 1.0.1

リリース作成

gh release create 1.0.1 .\rust-project\sample-app\target\release/sample-app.exe --title "特に何の意味もない版上げ" --notes-file ../release-notes.md --repo miyatama/learn-gh-cli
https://github.com/miyatama/learn-gh-cli/releases/tag/1.0.1

08.png

作って速攻消す

$ gh release delete 1.0.1 --repo miyatama/learn-gh-cli --yes
✓ Deleted release 1.0.1
! Note that the 1.0.1 git tag still remains in the repository

確認すると1.0.1が消えている

gh release list --repo miyatama/learn-gh-cli --json "tagName,name,createdAt,publishedAt"
[
  {
    "createdAt": "2025-08-11T02:52:12Z",
    "name": "初リリースバージョン",
    "publishedAt": "2025-08-11T04:26:27Z",
    "tagName": "1.0.0"
  }
]

gist の作成、編集、一覧表示、表示、削除

作成

適当なgistを作って編集。ここではgh-cli-sample.sh

gh-cli-sample.sh
#!/bin/bash
gh issue list --repo miyatama/learn-gh-cli
gh pr list --repo miyatama/learn-gh-cli
gh release list --repo miyatama/learn-gh-cli
gh workflow list --repo miyatama/learn-gh-cli
gh gist create gh-cli-sample.sh --desc "gh gistお試し" --public
- Creating gist gh-cli-sample.sh
✓ Created public gist gh-cli-sample.sh
https://gist.github.com/miyatama/89e0f868d72e36d8e1c1a0afc79839d7

一覧表示->編集->表示

gh gist list
ID                                DESCRIPTION                                      FILES   VISIBILITY  UPDATED
89e0f868d72e36d8e1c1a0afc79839d7  gh gistお試し                                    1 file  public      about 1 minute ago

内容表示

gh gist view 89e0f868d72e36d8e1c1a0afc79839d7
gh gistお試し

#!/bin/bash
gh issue list --repo miyatama/learn-gh-cli
gh pr list --repo miyatama/learn-gh-cli
gh release list --repo miyatama/learn-gh-cli
gh workflow list --repo miyatama/learn-gh-cli

gistのファイルを編集

#!/bin/bash

##################################################
# gh command examples
##################################################
repository_name="miyatama/learn-gh-cli"
gh issue list --repo ${repository_name}
gh pr list --repo ${repository_name}
gh release list --repo ${repository_name}
gh workflow list --repo ${repository_name}

編集をgithubへ適用して再表示

gh gist edit 89e0f868d72e36d8e1c1a0afc79839d7 gh-cli-sample.sh
gh gist view 89e0f868d72e36d8e1c1a0afc79839d7

結果

09.png

削除

作ったので早速削除

$ gh gist delete 89e0f868d72e36d8e1c1a0afc79839d7 --yes
✓ Gist "gh-cli-sample.sh" deleted

ふりかえり

最近Qiita触ってなかったから久々に重い腰を上げてみた。三連休の最後にしてはちゃんと記事も出せたから満足。

3
0
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?