Reputation: 339
I created two ssh-keys for different project in two github account. One is for mine with my account and another is for company with company account github. I worked on projects with different ssh-key. So everytime when I open different project I typed this command.
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa_my_git
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa_company_git
I hope to avoid typing this every time by adding a config file to the project or pc or using another way.
Upvotes: 4
Views: 7950
Reputation: 1
Add this in your config file:
Host work
HostName github.com
User git
IdentityFile ~/.ssh/id_work
Host personal
HostName github.com
User git
IdentityFile ~/.ssh/id_personal
Upvotes: 0
Reputation: 530960
You only have to run each of the three commands once; you can put them in whatever login shell configuration file you have (e.g., .bash_profile
).
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa_my_git
ssh-add ~/.ssh/id_rsa_company_git
Now, when you use git
, ssh
will simply try any key it finds in the agent until it finds one that works.
This is fine as long as you don't have so many keys to try that the remote end won't stop you after X failed attempts, before you can try the correct key. In that case, you can modify your ssh
configuration to create an alias for GitHub that uses a single specific key, so that ssh
can always use the correct key the first time. This approach also eliminates the need to use ssh-agent
at all, though you might still want to if you have a passphrase and only want to type it once, when you first load the key into the agent.
For example, you could put the following in your ~/.ssh/config
file.
Host github-me
Hostname github.com
IdentityFile id_rsa_my_git
Host github-company
Hostname github.com
IdentityFile id_rsa_company_git
Then you would use commands like
git clone git@github-me:me/personal_project
git clone git@github-company:company/work_project
and ssh
will know which key to use based on the host name.
Upvotes: 8