GitHub CLI で GitHub の定型操作が楽になったので、よく使うでコマンドを覚書としてまとめた。
設定方法
以下の記事見てね(いきなり引用でごめん)
昔 GitHub の設定周りで ↓の記事書いたけど、GitHub CLI だとここら辺の操作が楽になってて感動。
使い方
自分が使うやつ中心にまとめる。
issue のリストを表示する
表示する系のコマンドは issue も PR もだいたい同じような使い勝手。
# 全ての issue を表示 gh issue list # ラベルで issue を検索 gh issue list -l "good-first-issue" # author で issue を検索 gh issue list -A nukopy # assignee で issue を検索(よく使う) gh issue list -a nukopy # もちろん組み合わせも:ラベルと assignee gh issue list -l "good-first-issue" -a nukopy # 検索クエリで issue を検索(Web 上でよく使うやつ) gh issue list --search "error no:assignee sort:created-asc" # リポジトリの issues ページをブラウザで開く gh issue list -w
issue を作成する
gh issue create --help
の EXAMPLE を参照。これ見ればおおよそわかる。
gh issue create --title "I found a bug" --body "Nothing works" gh issue create --label "bug,help wanted" gh issue create --label bug --label "help wanted" gh issue create --assignee monalisa,hubot gh issue create --assignee "@me" gh issue create --project "Roadmap"
PR のリストを表示する
gh issue list --help
の EXAMPLE を参照。これ見ればおおよそわかる。
# List PRs authored by you gh pr list --author "@me" # List only PRs with all of the given labels gh pr list --label bug --label "priority 1" # Filter PRs using search syntax gh pr list --search "status:success review:required" # Find a PR that introduced a given commit gh pr list --search "<SHA>" --state merged
PR を作成する
gh pr create --help
の EXAMPLE を参照。これ見ればおおよそわかる。
gh pr create --title "The bug is fixed" --body "Everything works again" gh pr create --reviewer monalisa,hubot --reviewer myorg/team-name gh pr create --project "Roadmap" gh pr create --base develop --head monalisa:feature
PR のブランチにチェックアウトする
gh pr checkout {<number> | <url> | <branch>} [flags]
で特定の PR のブランチへチェックアウトできる(PR のリモートブランチをローカルに持ってくる)。ちなみにデフォルトで追加されている alias を使って gh pr co <number>
と書ける。
よくあるのは、gh pr list
で PR の番号を確認 → gh pr co <number>
で特定の PR のブランチへチェックアウトする流れ。
PR で走っている CI の状態を確認する
各ジョブの状態とジョブの Web ページへのリンクが表示されるので便利。
gh pr checks
alias を作る
gh alias set
コマンドを使う。gh alias set --help
の EXAMPLE を以下に示す。
# note: Command Prompt on Windows requires using double quotes for arguments gh alias set pv 'pr view' gh pv -w 123 #=> gh pr view -w 123 gh alias set bugs 'issue list --label=bugs' gh bugs gh alias set homework 'issue list --assignee @me' gh homework gh alias set 'issue mine' 'issue list --mention @me' gh issue mine gh alias set epicsBy 'issue list --author="$1" --label="epic"' gh epicsBy vilmibm #=> gh issue list --author="vilmibm" --label="epic" gh alias set --shell igrep 'gh issue list --label="$1" | grep "$2"' gh igrep epic foo #=> gh issue list --label="epic" | grep "foo"
試しに gh browse
を gh bw
で呼び出せるようにしてみる。
# 現在登録されている alias を表示する gh alias list # co はデフォルトで登録されている alias # co: pr checkout # alias を追加 gh alias set bw "browse" #- Creating alias for bw: browse #✓ Added alias bw # alias に登録されていることを確認 gh alias list # bw: browse # co: pr checkout # alias を実行 gh bw # 無事ブラウザでリポジトリが開かれた
リポジトリ、issue、PR をブラウザで表示する
コマンドラインから直接リポジトリ、issue、PR のページをブラウザで表示できる便利なコマンドたち
リポジトリのページをブラウザで表示する
これは便利
gh browse
issue のページをブラウザで表示する
# issue 一覧ページ gh issue list -w # 特定の issue 番号のページ gh issue view 1 -w
PR のページをブラウザで表示する
# PR 一覧ページ gh pr list -w # 特定の PR 番号のページ gh pr view 1 -w
よく使う gh コマンドの流れ
git clone の代わり
gh repo clone <user name>/<repo name> # 例 gh repo clone nukopy/rust-renshu-cho
リポジトリページの "< > Code" というボタンをクリックし、"GitHub CLI" というタブを選択すると、GitHub CLI を使ってリポジトリを git clone
するコマンドが表示される。
タスク始めるとき
- issue をブラウザ上で作成:
gh issue create -w
(issue 番号が 7 だとする) - PR 用のブランチを作る & checkout:
git switch -c issues/7-fix-error-handling-on-connecting-to-nats
- push する用の空コミット:
git commit --allow-empty -m "Start issue #7"
- push を同時に行うオプションを選択した時、ブランチ上にコミットが存在しないと PR の作成が失敗する(後述)
- PR を作成:
gh pr create -t "Fix error handling on connecting to NATS" -b "Close #7"
補足:push を同時に行うオプションを選択した時、ブランチ上にコミットが存在しないと PR の作成が失敗する
- 失敗例
➜ gh pr create -t "chap01" Warning: 1 uncommitted change ? Where should we push the 'chap01' branch? nukopy/rust-renshu-cho Creating pull request for chap01 into main in nukopy/rust-renshu-cho ? Body <Received> ? What's next? Submit remote: remote: To github.com:nukopy/rust-renshu-cho.git * [new branch] HEAD -> chap01 branch 'chap01' set up to track 'origin/chap01'. pull request create failed: GraphQL: No commits between main and chap01 (createPullRequest)
- 成功例
➜ gh pr create -t "chap01" Warning: 1 uncommitted change ? Where should we push the 'chap01' branch? nukopy/rust-renshu-cho Creating pull request for chap01 into main in nukopy/rust-renshu-cho ? Body <Received> ? What's next? Submit To github.com:nukopy/rust-renshu-cho.git 40baa40..72ca78a HEAD -> chap01 branch 'chap01' set up to track 'origin/chap01'. https://github.com/nukopy/rust-renshu-cho/pull/1
issue 作ってそれをベースに PR 作って、、ってのは以下の記事の流れをベースにしてる。ここは開発チームの運用によりけり。
ここで上記の記事に言及するのもなんだけど、作業中の PR のタイトルに WIP 入れるより、PR の Draft 機能を使った方良い(gh pr create -d
というオプションでドラフト PR を作成できる)。これも開発チームの GitHub の運用によりけり。
Githubで作業中のPRのタイトルにWIPを入れる文化は根強いけど、今はDraft機能を使った方が良いです
— そな太 (@sonatard) July 14, 2020
DraftはMergeがブロックされます pic.twitter.com/wMhOmchZwj
他の人の PR を見る
- メールか Slack で PR のリンクが飛んでくる
- ブラウザで PR を表示する & PR の番号確認
- PR のブランチへチェックアウト:
gh pr co <PR number>
- コード読んだり、動かしたり
使ってる alias
alias 一覧
$ gh alias list bw: browse co: pr checkout prvw: pr view -w
alias 作るコマンド
gh alias set bw "browse" gh alias set prvw "pr view -w"