Githubにおける複数アカウントの利用

[追記]よく探したら解決法がHelpにありました。ホストを仮想のものにして、pushなどのときには各々のホストを指定するようです。一番下にも追記あり

Githubで複数のアカウントを使用するときにハマったので備忘録。どうしたもんか、以下のエントリに書いてあるようにやってもうまくいかなかった*1
GitHubで複数アカウントを使うときの鍵の設定 - 思っているよりもずっとずっと人生は短い。

2つのアカウント用の鍵を生成

$ cd ~/.ssh/
$ ssh-keygen -t rsa -C "alice@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/foo/.ssh/id_rsa): /Users/foo/.ssh/alice
...
$ ls
alice alice.pub
$ ssh-add alice

$ ssh-keygen -t rsa -C "bob@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/foo/.ssh/id_rsa): /Users/foo/.ssh/bob
...
$ ls
alice alice.pub bob bob.pub
$ ssh-add bob

公開鍵の設定

githubの設定画面でそれぞれのアカウント用の公開鍵を設定する。説明省略。

Aliceのアカウントを使うとき

~/.ssh/configを以下のように修正。

Host github.com
 User git
 Port 22
 Hostname github.com
 IdentityFile /Users/foo/.ssh/alice
 TCPKeepAlive yes
 IdentitiesOnly yes

試しにssh接続してみると…

$ ssh git@github.com
PTY allocation request failed on channel 0
ERROR: Hi Alice! You've successfully authenticated, but GitHub does not provide shell access
              Connection to github.com closed.

成功。Bobのアカウントを使用するときは~/.ssh/configのIdentityFileをbobのに変える。

疑問

  • 上記エントリにあるようにIdentityFileを2行続けて書いても適切な方の公開鍵を自動で選んでくれない?
    • 選んでくれないみたい
  • commitのauthor nameとかはgit-configで設定したものになる?(github.comのアカウント名は関係ない?)
    • Networkグラフではgit-configで設定したuser.nameが表示される。user.emailがgithubアカウントに登録されたものと一致すればそのアバターが表示される

WEB+DB PRESS Vol.50

WEB+DB PRESS Vol.50

*1:自分がミスってる可能性も?