As a Developer chances are that you may need to use multiple Git accounts on the same machine, e.g. Github, Gitlab, Bitbucket, but using the default settings may mess up your accounts an permissions. This post aim to help you configure multiple Git acounts on your workstation so youcan use them in a painless way.

Lets supose we have 3 different accounts: personal, work and client. First we need to have a independent SSH key for each one. We can generate them using the following snippet:

ssh-keygen -t rsa -b 4096

We should generate a SSH key pair for each account, e.g. ~/.ssh/id_rsa_personal and ~/.ssh/id_rsa_work, ~/.ssh/id_rsa_client. Then we should add the proper public keys to each Git account.

Then we need to define the proper settings in the ~/.ssh/config file which should look like this:

# personal account
Host github.com-personal
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_personal

# work account
Host github.com-work
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_work

# client account
Host github.com-client
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_rsa_client

With this configuration now we can explicitly use any account we want. For example, lets supose we need to clone my personal Ghost Docker Compose project (the one I use for running this site):

git clone git@github.com-personal:aimatus/ghost-docker-compose.git

Pay attention to the -personal suffix after git@github.com (the same we defined in our ~/.ssh/config file). This will automatically select the proper SSH key for us and should be enough for pulling/pushing for the rest of the project life. The same should be done for work or client projects.

I hope this post will help you handling multiple Git accounts with their respective SSH keys.