(Learn log) Use multiple ssh keys with git
I needed to create multiple ssh keys with github/ bitbucket accounts.
The steps i followed :
- Create ~/.ssh/config file
-
Generate multiple identities (one for bitbucketwork, one for bitbucketpersonal)
ssh-keygen -f ~/.ssh/bitbucketwork -C “bitbucketwork” repeat for bitbucketpersonal
-
Create a host entry for each identity
Host bitbucketwork HostName bitbucket.org IdentityFile ~/.ssh/workdid Host bitbucketpersonal HostName bitbucket.org IdentityFile ~/.ssh/personalid
- Change the git remote to use the correct alias of your identity
For example, i have a work repository, to make git use the work identity
Before:
git@bitbucket.org:accountname/reponame.git
After:
git@bitbucketwork:accountname/reponame.git
Where bitbucketwork is one of the Host alias previously created
-
Paste the public key in the bitbucket settings
pbcopy < ~/.ssh/id_rsa.pub
-
Add the key(s) to the ssh agent with :
ssh-add ~/.ssh/work
( Note: Add to .bash_profile, otherwise you will have to add the key everytime you open a terminal )
References
[https://gist.github.com/jexchan/2351996] [https://confluence.atlassian.com/pages/viewpage.action?pageId=271943168]