LoginSignup
0
0

More than 1 year has passed since last update.

gitで秘密鍵を使い分ける

Posted at

gitで cloneやpushをする際に何故か違う鍵が使われて詰まったので

configで設定するやり方

.ssh/config
Host <呼ぶときの名前>
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/<使う鍵(秘密鍵の方)>
  TCPKeepAlive yes
  IdentitiesOnly yes #これがないと他の鍵(デフォルトのid_rsaなど)も読み込まれてしまう

を作成し

git clone git@<configで設定した「呼ぶときの名前」>:<githubのユーザー名>/<githubのレポジトリ名>.git

を叩く
参考ソース:
https://zenn.dev/hironorioka28/articles/35a287ffbcf533

設定ファイルなしのコマンドのみで実行する場合

GIT_SSH_COMMAND="ssh -i ~/.ssh/<鍵名> -o IdentitiesOnly=yes" git clone git@github.com:<ユーザー名>/<レポジトリ名>.git

-o でオプションを指定出来る
ここでconfigと同様にデフォルトの鍵(id_rsa)が呼ばれないように指定する
参考ソース:
https://stackoverflow.com/questions/4565700/how-to-specify-the-private-ssh-key-to-use-when-executing-shell-command-on-git#answer-29754018

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