UbuntuにGitHub CLIをインストール
GitHub CLI をインストールする方法を紹介します。
要約
参考
gh をインストール
以下のコマンドを実行します。
sudo apt updatesudo apt install git gh
最新版をインストールしたい場合は以下のコマンドを実行します。
(type -p wget >/dev/null || (sudo apt update && sudo apt-get install wget -y))sudo mkdir -p -m 755 /etc/apt/keyringswget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/nullsudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpgecho "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/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 git gh
認証
以下のコマンドを実行して認証を行います。
gh auth login
認証では、ブラウザを使用するか、トークンを入力するかを選択します。
プライベートリポジトリのクローン
認証時に、HTTPS
プロトコルを選択した場合は
git clone https://github.com/USERNAME/REPO.git
SSH
プロトコルを選択した場合は
git clone git@github.com:USERNAME/REPO.git
で、クローン可能です。 また、どちらのプロトコルを選択した場合でも以下のコマンドでクローンできます。
gh repo clone USERNAME/REPO
API を使用する
ユーザーの情報を取得する
gh api user
{ "login": "USERNAME", "id": XXXXXXXXX, "node_id": "XXxxXX", "avatar_url": "https://avatars.githubusercontent.com/u/XXXXXXXXX?v=4",29行の折りたたみ
"gravatar_id": "", "url": "https://api.github.com/users/USERNAME", "html_url": "https://github.com/USERNAME", "followers_url": "https://api.github.com/users/USERNAME/followers", "following_url": "https://api.github.com/users/USERNAME/following{/other_user}", "gists_url": "https://api.github.com/users/USERNAME/gists{/gist_id}", "starred_url": "https://api.github.com/users/USERNAME/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/USERNAME/subscriptions", "organizations_url": "https://api.github.com/users/USERNAME/orgs", "repos_url": "https://api.github.com/users/USERNAME/repos", "events_url": "https://api.github.com/users/USERNAME/events{/privacy}", "received_events_url": "https://api.github.com/users/USERNAME/received_events", "type": "User", "site_admin": false, "name": null, "company": null, "blog": "", "location": null, "email": null, "hireable": null, "bio": null, "twitter_username": null, "public_repos": 0, "public_gists": 0, "followers": 0, "following": 0, "created_at": "20XX-XX-XXTXX:XX:XXZ", "updated_at": "20XX-XX-XXTXX:XX:XXZ"}
上記以外にも、アカウントによってowned_private_repos
やplan
などの項目が含まれることがあります。
メールアドレスの公開設定を確認する
gh api user/emails
以下のようなエラーが発生した場合は
{ "message": "Not Found", "documentation_url": "https://docs.github.com/rest/users/emails#list-email-addresses-for-the-authenticated-user", "status": "404"}gh: Not Found (HTTP 404)gh: This API operation needs the "user" scope. To request it, run: gh auth refresh -h github.com -s user
メッセージ通りにコマンドを実行します。
gh auth refresh -h github.com --scopes user
認可が終了したら、再度gh api user/emails
コマンドを実行します。デフォルトでは以下のような返答が得られます。
[ { "email": "YOUR_EMAIL@example.com", "primary": true, "verified": true, "visibility": "public" }]
メールアドレスの公開設定を変更する
メールアドレスの公開設定をprivate
に変更します。
gh api \ --method PATCH \ -H "Accept: application/vnd.github+json" \ -H "X-GitHub-Api-Version: 2022-11-28" \ /user/email/visibility \ -f "visibility=private"
以下のような返答が得られます。
[ { "email": "YOUR_EMAIL@example.com", "primary": true, "verified": true, "visibility": "private" }]
また、メールアドレスの公開設定をprivate
にすると、先ほどのコマンドの実行結果も変わります。
gh api user/emails
[ { "email": "YOUR_EMAIL@example.com", "primary": true, "verified": true, "visibility": "public" }, { "email": "USERID+USERNAME@users.noreply.github.com", "primary": false, "verified": true, "visibility": null }]
設定
ローカル環境の git の設定(名前、メールアドレス)を GitHub 側に合わせる際に、以下のように API を使用することもできます。
GITHUB_USER=$(gh api user --jq .login)echo "GITHUB_USER: $GITHUB_USER"git config --global user.name "$GITHUB_USER"
GITHUB_EMAIL=$(gh api user/emails --jq '.[0].email')# 非公開設定# GITHUB_EMAIL=$(gh api user/emails --jq '.[1].email')echo "GITHUB_EMAIL: $GITHUB_EMAIL"git config --global user.email "$GITHUB_EMAIL"
メールアドレスとして、gh api user/emails --jq '.[1].email'
を使用すると、git log
コマンドを実行した際に表示されるAuthor
のメールアドレスがUSERID+USERNAME@users.noreply.github.com
になります。
自身のメールアドレスを知られたくない場合に設定するとよいと思います。