In this short guide two sample user will be used: leisure and work.
-
Create two ssh keys, one of each user, and register them with their github account on the site. Let's suppose you have created
id_rsa_work
andid_rsa_leisure
. -
Configure ssh using two virtual hosts, one for work and one for leisure, each one linked to one of the keys:
# Personal GitHub account Host leisure-github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa_leisure PreferredAuthentications publickey PasswordAuthentication no IdentitiesOnly yes # Work GitHub account Host work-github.com HostName github.com User git IdentityFile ~/.ssh/id_rsa_work PreferredAuthentications publickey PasswordAuthentication no IdentitiesOnly yes
Every time you'll want to use the leisure account clone the repo using
leisure-github.com
instead ofgithub.com
. Usework-github.com
for the work related account. -
Now we need to instruct git to use a different account for different repositories. You could just set a local configuration with different username/email each time you download a new repo (perhaps through an alias) but a better idea is to add a different automatic configuration for repositories downloaded in a specific directory. In this example, let's suppose you always want to use the work account except in repositories cloned under
~/FunRepos/
. Structure your global~/.gitconfig
this way and create a new~/.gitconfig-leisure
:[user] name = Work User email = user@work.com signingkey = 123456789 [github] user = work token = 123456787654323456765432 [commit] gpgsign = true .... .... end of .gitconfig .... [includeIf "gitdir:~/FunRepos/"] path = .gitconfig-leisure
Content of .gitconfig-leisure
:
[user]
name = Leisure User
email = "user@fun.com"
signingkey =
[github]
user = Leisure
token =
[commit]
gpgsign = false
All repositories cloned under ~/FunRepos
will use the second configuration.
It's probably necessary to override the main configuration here if you want to reset some parameters (int his case done for signingkey, token, etc...)
That's it.