GOAL: To setup work and personal github account in the same machine. The gist assumes that you already have the ssh key for work and personal use.
Edit ~/.ssh/config
and add an entry for your work and personal github account with different keys:
# Personal Account
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/personal_rsa
IdentitiesOnly yes
# Work Account
Host work.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/work_rsa
IdentitiesOnly yes
Cloning a personal repository
You can clone as usual:
git clone [email protected]:user/repo.git
Cloning a work repository
When cloning, you need to change the url to work.github.com
git clone [email protected]:user/repo.git
Edit ~/.gitconfig
and add a conditional config loaded based on working directory.
[includeIf "gitdir:/Users/yajra/work/"]
path = ~/.gitconfig-work
[includeIf "gitdir:/Users/yajra/personal/"]
path = ~/.gitconfig-personal
Create 2 gitconfig files for work and personal setup
~/.gitconfig-work
[user]
name = John Doe
email = [email protected]
[core]
sshCommand = ssh -i ~/.ssh/work_rsa
~/.gitconfig-personal
[user]
name = John Doe
email = [email protected]
[core]
sshCommand = ssh -i ~/.ssh/personal_rsa
- All personal repositories should be cloned inside
/Users/yajra/personal
folder. - Work related repositories should be cloned inside
/Users/yajra/work
folder.
Using solution #2, committer name and email will adjust accordingly and there is no need to update the clone URL everytime we clone a work repository.