How to use multiple SSH keys

Because you cannot use the same SSH key for multiple accounts on Github, Gitlab, Bitbucket, etc, you will need to generate multiple public/private keys and use them accordingly.

Let's create a ssh key

ssh-keygen -t rsa -C "user-1@example.com"

You will be prompted for an optional password. It depends on you if you want to have a password or not for this.

Copy the public key (Mac OS)

pbcopy < ~/.ssh/id_rsa.pub

And paste it in your git server account SSH keys page. You may see here how

If you use only one account, then you can stop here.

Generate a new SSH key with a different name

ssh-keygen -t rsa -f ~/.ssh/second-account -C "user-2@example.com"

Now, you need to create the file ~/.ssh/config with the following content:

Host bitbucket.org
	User git
    Hostname bitbucket.org
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa

Host bitbucket-second
    User git
    Hostname bitbucket.org
    PreferredAuthentications publickey
    IdentitiesOnly yes
    IdentityFile ~/.ssh/second-account

Now, to use the primary ssh key to clone a project you use the following command

git clone git@bitbucket.org:username/project-name.git

And for the second account you'll have to change the hostname in the url like this:

git clone git@bitbucket-second:username/project-name.git
The example was provided with an example to bitbucket, but in the same way it works for Gitlab, Github and any other git servers.