gitの複数アカウント設定方法

➜ ~ cd .ssh

秘密鍵、公開鍵を作成する

➜ .ssh ssh-keygen -t rsa -C “email@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/username/.ssh/id_rsa): id_rsa.firstname
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

➜ .ssh ssh-keygen -t rsa -C “email@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/username/.ssh/id_rsa): id_rsa.secondname
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

sshのコンフィグの設定

➜ .ssh vim config

  1 Host firstname.github.com
  2     HostName github.com
  3     User git
  4     PreferredAuthentications publickey
  5     IdentityFile ~/.ssh/id_rsa.firstname
  6 
  7 Host secondname.github.com
  8     HostName github.com
  9     User git
 10     PreferredAuthentications publickey
 11     IdentityFile ~/.ssh/id_rsa.secondname

git haveに登録

➜ pbcopy < ~/.ssh/id_rsa.firstname.pub
➜ pbcopy < ~/.ssh/id_rsa.secondname.pub

アクセステスト

➜ .ssh ssh -T firstname.github.com
Hi firstname! You've successfully authenticated, but GitHub does not provide shell access.
➜ .ssh ssh -T secondname.github.com
Hi secondname! You've successfully authenticated, but GitHub does not provide shell access.

git clone

いつものgit cloneの仕方

git clone git@github.com:gitname/repositories.git

複数使う場合

git clone git@firstname.github.com:gitname/repositories.git
git clone git@secondname.github.com:gitname/repositories.git

結果

➜  repositories git:(master) cat .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[remote "origin"]
	url = git@firstname.github.com:gitname/repositories.git 
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master

ここが変わる url = git@firstname.github.com:gitname/repositories.git
すでにCloneして、後で変更したい場合はここを変更する。

コミットするアカウントを変更したい場合

➜ repositories git:(master) git config user.name "firstname"
➜ repositories git:(master) git config user.email "email@example.com"

参考

gist.github.com