Configuring Separate SSH Keys for GitHub and GitLab on Windows
This guide explains how to generate, add, and test distinct SSH keys for GitHub and GitLab, configure a SSH config file to use the appropriate key for each host, and includes all necessary commands and screenshots for a smooth setup.
When working on personal projects hosted on GitHub while company projects reside on GitLab, a single SSH key is insufficient; you need separate keys to avoid authentication issues.
First, verify any existing SSH keys by running ls -al ~/.ssh . If no keys are present, you will see an empty directory.
GitHub typically recognizes public key files named id_rsa.pub , id_ecdsa.pub , or id_ed25519.pub .
Generate a new SSH key with the command ssh-keygen -t ed25519 -C "[email protected]" . This creates a private key and a corresponding .pub public key in the ~/.ssh folder.
Open your GitHub account settings, navigate to the SSH keys section, and paste the contents of the newly created .pub file into a new key entry.
Test the connection by executing ssh -T [email protected] in the terminal; a successful response will include a greeting such as “Hi username!”.
To add a second key for GitLab, repeat the key generation step, but rename the key files (e.g., lab_ed25519 ) to avoid name collisions. Add the new public key to your GitLab account in the same manner.
Finally, create a config file inside ~/.ssh with the following content to tell Git which key to use for each host:
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_ed25519
Host gitlab.com
HostName gitlab.com
IdentityFile ~/.ssh/lab_ed25519With this configuration, Git will automatically select the correct SSH key when interacting with either GitHub or GitLab, allowing you to clone, push, and pull from both services without conflict.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.