Multiple SSH keys configuration for GitHub and GitLab accounts
If you have multiple GitLab or GitHub accounts (for example, a personal account and a company account) and each of them uses a different SSH key (which is a good practice), you can define a separate authentication configuration for each account. This allows your development environment to automatically determine which SSH key should be used in a given context.
In practice, this means that operations performed on different repositories, whether they belong to GitLab, GitHub, or another platform, will use the correct identity without the need to manually switch between SSH keys. Such a configuration ensures a stable workflow, eliminates issues caused by incorrect key selection, and enables smooth management of multiple developer profiles within a single environment.
Default SSH keys configuration
To configure default SSH keys, follow these steps:
1. Create the config file (if it does not already exist) in the .ssh directory, which is usually located in the (hidden) .ssh subfolder at the following path:
Linux/Mac: %HOMEDRIVE%%HOMEPATH%/.ssh/ (in my case it looks like this: home/myusername/.ssh )
Windows: %USERPROFILE%/.ssh/ (in my case it looks like this: c:\users\myusername\.ssh )
Keep in mind that this path depends on your system configuration.
2. In the config file, define the host names and specify the locations of the individual keys.
Host work.gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentitiesOnly yes
IdentityFile /path/to/.ssh/id_gitlab_work
Host private.gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentitiesOnly yes
IdentityFile /path/to/.ssh/id_gitlab_private
Host – an arbitrary name, you can enter anything you like here.
HostName – one of the GitLab hostnames. Other valid hostnames can be found here: https://docs.gitlab.com/ee/user/gitlab_com/#hostname-list
PreferredAuthentications – specifies that GitLab will use public key authentication. You must add the corresponding public key to your GitLab account here: https://gitlab.com/-/user_settings/ssh_keys
IdentitiesOnly – indicates that the connection should use only the key specified in IdentityFile for authentication.
IdentityFile – specifies the path to the SSH key file.
Cloning a repository using the appropriate SSH key
To clone a repository from GitLab, you need to specify the appropriate SSH key so that the correct identity is used for authentication with the remote repository.
git clone [email protected]:namespace/my-project-3.git --config core.sshCommand="ssh -i /path/to/.ssh/id_work"
What should you do if you already have a project set up locally and only need to add a remote repository? In that case, you can use the following command:
git remote set-url origin [email protected]:namespace/my-project-3.git
Notice that the standard project URL is [email protected]:namespace/my-project-3.git, and in the example above we replaced the hostname gitlab.com with work.gitlab.com.
The same approach works for GitHub as well.
You can now make commits using different SSH keys for different projects 🙂