GitHub で SSH Key が設定してあってもパスワードが聞かれる場合の対応策

結論

まず、macOS と Linux で対応が少し異なる。

macOS の場合

~/.ssh/config への記述内容として、AddKeysToAgent yes という行と、UseKeychain yes という行を含める。

Host github.com
  HostName github.com
  Port 22
  User git
  IdentityFile ~/.ssh/github_secret_key
  AddKeysToAgent yes
  UseKeychain yes

Linux の場合

~/.ssh/config への記述内容として、UseKeychain yes を記入してはいけない*1。それ以外は macOS の場合と同じである。

Host github.com
  HostName github.com
  Port 22
  User git
  IdentityFile ~/.ssh/github_secret_key
  AddKeysToAgent yes

あとは

上述の各設定が済めば、リポジトリ内の .git/config における [remote "origin"] のアドレスの設定を SSH にすれば OK。

[core]
(省略)
[remote "origin"]
    url = git@github.com:your_name/your_repository.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
(省略)

最初に git clone をする際に、SSH で git clone をすれば、[remote "origin"] のアドレスは SSH になっている*2

あやふやな情報

macOS だと SSH でなくてもパスが聞かれなかったような記憶がある*3

*1:「Bad configuration option: usekeychain」と怒られる

*2:もちろん SSH で git clone するためには予め 鍵 が設定されていなければならない

*3:信頼性低いし、厳密な条件も不明

Powered by はてなブログ