Architect's Log

I'm a Cloud Architect. I'm highly motivated to reduce toils with driving DevOps.

GitHub認証にGitHub CLIを利用するには

コマンドラインから GitHub にアクセスする際に、認証する方法は複数あります。

GitHub CLI (gh) を使用しブラウザで認証すると、persnal access token の発行が不要になります。プロトコルは SSH も選べますが、今回は鍵の作成が不要な HTTPS を使用します。

環境

Ubuntu 24.04.3 LTS on WSL

手順

GitHub CLI (gh) をインストールします。

$ sudo apt update
$ sudo apt install gh                                                                                 
$ gh --version
# gh version 2.45.0 (2025-07-18 Ubuntu 2.45.0-1ubuntu0.3)                                                                                                                      ```

gh auth login を実行し、以下のように入力します。ブラウザで提示された URL にアクセスし、認証コードを入力します。

$ gh auth login
? What account do you want to log into? GitHub.com
? What is your preferred protocol for Git operations on this host? HTTPS
? Authenticate Git with your GitHub credentials? Yes
? How would you like to authenticate GitHub CLI? Login with a web browser

gh が Git の credential helper として設定され、今後 git clone 等の際に認証が不要になります。

$ git config --list | grep credential
# credential.https://github.com.helper=
# credential.https://github.com.helper=!/usr/bin/gh auth git-credential
# credential.https://gist.github.com.helper=
# credential.https://gist.github.com.helper=!/usr/bin/gh auth git-credential

参考

https://docs.github.com/ja/authentication/keeping-your-account-and-data-secure/about-authentication-to-github