ssh認証が別のgitアカウントを使い分ける方法メモ
1. configファイルを作成する
vim ~/.ssh/config
Host gitmain HostName bitbucket.org IdentityFile ~/.ssh/id_rsa User namemain Port 22 TCPKeepAlive yes IdentitiesOnly yes Host gitsub HostName bitbucket.org IdentityFile ~/.ssh/sub_id_rsa User namesub Port 22 TCPKeepAlive yes IdentitiesOnly yes
Hostにつけた名前が、後からコマンドで指定する識別子になる。
IdentityFileとUserが、実際にユーザを切り替えて使う際のキー。
2. 接続確認
Host名を指定してssh -Tして、以下のような応答が返ってくればok。
ssh -T gitmain ↓ authenticated via ssh key. You can use git to connect to Bitbucket. Shell access is disabled
3. gitのユーザ名などのconfigを切り替える
以下のコマンドは大抵開発PCの環境構築の最初にしか使わないが、アカウントを使い分ける場合は常時打つことになる。
sh化しておくといいかも。
git config --global user.name "namemain" git config --global user.email "dummy@example.com"
4. git clone例
ポイントは、cloneで指定するhost名を1で作成したconfigのHost識別子に変えること。それだけ。
git clone git@gitmain:hoge/fuga.git
以上。