Git Tips - Using multiple SSH keys with Git | Git in-use

Posted by Phong To on Sat, Feb 18, 2023

Using Multiple SSH Keys for Git

When you work with Git, you might need to use different SSH keys for different repositories. For example, you might have personal and work Git accounts, each with their own SSH keys. In this case, you need to tell Git which key to use for which repository.

One way to do this is to use SSH configuration files. These files allow you to specify the SSH key and other options to use when connecting to a specific host. In this blog post, we’ll show you how to use SSH configuration files to manage multiple SSH keys for Git

Generate a new SSH key pair

First, generate a new SSH key pair for each Git account or repository you want to use. You can use the ssh-keygen command to generate a new key pair.

1ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

The command varied by your Git client:

IMPORTANT: When the Terminal asking for the location and file name, you should give it a remarkable name for later easier usage.

Add your SSH keys to the SSH agent (Optional)

After generating your SSH key pairs, you can add them to the SSH agent so that Git can use them. To do this, run the following command:

1ssh-add /path/to/private/key

Add the public SSH key to your Git account or repository

To authenticate with Git using your SSH keys, you need to add the public key to your Git account or repository. You can copy the contents of the public key file (e.g., id_rsa.pub) and paste it into the SSH key settings of your Git account or repository.

Configure Git host URL to use the correct SSH key

To tell Git which SSH key to use for a particular repository or account, you need to configure the SSH URL of the repository to include the path to the private key. You can do this by editing the ~/.ssh/config file and adding the following configuration (create a new one if it does not existed):

1Host github.com
2  HostName github.com
3  User git
4  IdentityFile /path/to/private/key
5
6Host gitlab.com
7  HostName gitlab.com
8  User git
9  IdentityFile /path/to/other/private/key

You need to replace the Host values with the SSH URL of the repository or account you want to use, and the IdentityFile value with the path to the private key for that account.

Test your SSH connection

To test that your SSH connection is working, you can run the following command:

1ssh -T git@github.com

Replace github.com with the SSH URL of the repository or account you want to test. If the SSH connection is working, you should see a message that says “Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.”

That’s it! You should now be able to use multiple SSH keys with Git. When you clone or push to a repository, Git will use the SSH key that matches the SSH URL of the repository.

EXTRA TIP: Using config with multiple Git accounts associated with same host

If you have multiple Git accounts associated with the same github.com host, you can still use the Host keyword in your SSH configuration file. However, you’ll need to add a unique alias for each account to differentiate them.

Here’s an example configuration that shows how to use aliases to specify different SSH keys for multiple GitHub accounts:

1Host github.com-personal
2  HostName github.com
3  User git
4  IdentityFile ~/.ssh/id_rsa_personal
5
6Host github.com-work
7  HostName github.com
8  User git
9  IdentityFile ~/.ssh/id_rsa_work

In this example, we’ve created two Host sections: github.com-personal and github.com-work. The HostName and User options are the same for both accounts since they are both hosted on GitHub. However, the IdentityFile option is different for each account, specifying the path to the appropriate SSH key.

To clone or push to a repository, you’ll need to use the alias instead of the full hostname. For example, to clone a repository associated with your personal account, you would use:

1git clone git@github.com-personal:user/repo.git

Note that the alias (github.com-personal) is used instead of the full hostname (github.com) when specifying the SSH URL. You can similarly use the alias when pushing changes to a repository associated with your personal account.

Writer assistant: ChatGPT

Thanks for reading!



comments powered by Disqus