1つのパソコンで、複数のSSH鍵を使い分けてGitを操作したい

GitHub

1つのパソコンで、複数のSSH鍵を使い分けてGitを操作したい

流れ

  • 鍵作成
  • アカウント切り替え
  • クローン

鍵作成

鍵の作成はこちらの記事を参考にします。
鍵の名前が被らないように、任意の名前をつける必要があるので、
作成コマンドは下記のようにします。

-fオプションででファイル名を指定できます。

$ ssh-keygen -t ed25519 -f id_ed25519_sub

アカウントを切り替える

$ vi ~/.ssh/config

※ #部分は削除しておく

# メインのアカウント
Host github.com
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_ed25519
  TCPKeepAlive yes
  IdentitiesOnly yes

# サブのアカウント
Host github.com.sub
  HostName github.com
  User git
  Port 22
  IdentityFile ~/.ssh/id_ed25519_sub
  TCPKeepAlive yes
  IdentitiesOnly yes

SSH接続する

作成した秘密鍵id_ed25519_subを指定して接続する。

$ ssh git@github.com -i ~/.ssh/id_ed25519_sub
PTY allocation request failed on channel 0
Hi [user名]! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

You've successfully
接続成功しました。

クローン

@以降が普段の接続先と違うので注意。
サブアカウントでクローンしたい場合は、configで設定した、
サブアカウントを指定する。

$ git clone git@github.com.sub:sample/sample.git
Cloning into 'sample'...
remote: Enumerating objects: 7, done.
remote: Counting objects: 100% (7/7), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 7 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (7/7), done.

その後の基本操作は別の記事に。

まとめ

プライベートリポジトリを別のパソコンから操作することはできたけど、期待する動きにならなかった。。。。
(A)アカウントのパソコンから、別のアカウント(B)のGitを操作はできたが、なぜか、(A)としてコミットされてしまった。なんでだろう。

コメント