context

Github uses the email address to identify the user behind a specific commit.
If you import a ssh key with another email address, chances are the commits will not be linked to the proper account.

Here is the setup to use to be able to use several github account on the same machine using SSH keys

ssh config

generate a set off keys for each account with the following command

ssh-keygen -t rsa -C "primary-account@example.com"
ssh-keygen -t rsa -C "another-account@example.com"

Name the files so as to have in .ssh

id_rsa_primary
id_rsa_primary.pub
id_rsa_another
id_rsa_another.pub

create a .ssh/config file

touch ~/.ssh/config
chmod 644 ~/.ssh/config

and put the following in ~/.ssh/config

Host github-primary
HostName github.com
IdentityFile ~/.ssh/id_rsa_primary
User git

Host github-secondary
HostName github.com
IdentityFile ~/.ssh/id_rsa_another
User git

cloning a repository

Clone the repository as usual
and set the identity that should be used

git clone git@github.com:another/myproject.git
cd myproject
git config user.name "another user name"
git config user.email "another-account@example.com"

And you’re done, the rest is normal git flow

Advertisements