github の複数アカウント作成
github での複数アカウントの登録をしたときのメモです。
新しいアカウント用の SSH Key をつくる
まずは .ssh
ディレクトリに移動して、
$ cd ~/.ssh/
SSH Key を作成します。
$ ssh-keygen -t rsa -C 'mail-address@example.com'
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/me/.ssh/id_rsa): /Users/me/.ssh/id_rsa_second
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/me/.ssh/id_rsa_second.
Your public key has been saved in /Users/me/.ssh/id_rsa_second.pub.
The key fingerprint is:
7d:34:b3:a5:9e:b8:13:d4:d8:a9:4c:fc:ae:1d:a2:d1 mail-address@example.com
The key's randomart image is:
+--[ RSA 2048]----+
| |
| |
| o+... |
| ...=*o |
| S..++o |
| .=o.. |
| o.N.. |
| .+ o.. |
| o.... |
+-----------------+
どこに key を保存するかを途中で聞かれるので、現在使っている key とは別の名前になるように、ファイル名を指定してください。
Enter file in which to save the key (/Users/me/.ssh/id_rsa): /Users/me/.ssh/id_rsa_second
新しい SSH key を GitHub に登録
GitHub の2つ目のアカウントに、新しく作った SSH key を登録します。
$ cat id_rsa_second.pub
public key をコピーして、
GitHub の Acount settings の SSH keys ページから登録します。
SSH 接続する際のパスワード入力を省略
ssh-agent
を使って秘密鍵を登録して、
次回から SSH 接続するときにパスワードを入力する手間を省いておきます。
$ eval `ssh-agent`
Agent pid 4586
$ ssh-add ~/.ssh/id_rsa_second
Enter passphrase for id_rsa_second:
Identity added: id_rsa_second (id_rsa_second)
それぞれの SSH key のホスト名を設定
~/.ssh/config
ファイルにホスト名を設定します。
ファイルがなかったら作ってあげてください。
$ touch ~/.ssh/config
以下のようにホスト名を設定します。
$ vi ~/.ssh/config
# github first account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes
# github second account
Host github.com-second
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_second
IdentitiesOnly yes
ホスト名 Host github.com
Host github.com-second
のところは、
自分が区別がつく名前であればOKです。
GitHub に別アカウントとして認識されているかを確認
それぞれのホスト名を使って、GitHub に SSH 接続をしてみます。
$ ssh -T git@github.com
Hi user_name! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@github.com-second
Hi second_name! You've successfully authenticated, but GitHub does not provide shell access.
上記のように表示されれば大丈夫です。
アカウントを指定して git コマンドを使う
git コマンドでの操作は、それぞれ以下のようになります。
先ほどのホスト名を指定してあげるだけで OK です。
新しくリモートリポジトリを登録するとき:
$ git remote add origin git@github.com-second:hoge/repo_name.git
既存のリポジトリをクローンしてくるとき:
$ git clone git@github.com-second:hoge/repo_name.git
現在のリポジトリの置き場所(リモートリポジトリの URL)を変更するとき:
$ git remote set-url origin git@github.com-second:hoge/repo_name.git
$ git config -l | grep url
remote.origin.url=git@github.com-second:hoge/repo_name.git
(記事は、2012/05/18 時点での内容です)
[参考にしたURL]
★Quick Tip: How to Work with GitHub and Multiple Accounts - net.tutsplus.com
http://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/
★githubで複数ユーザを使い分ける - 文殊堂
http://d.hatena.ne.jp/monjudoh/20110411/1302521587
参考にさせていただきました。ありがとうございます。