DEV Community

Cover image for Managing Multiple SSH Keys for Different Git Hosting Platforms (GitLab & GitHub)
Weerayut Teja
Weerayut Teja

Posted on

Managing Multiple SSH Keys for Different Git Hosting Platforms (GitLab & GitHub)

If you have multiple GitHub or GitLab accounts, you might run into SSH key conflicts when pushing or pulling repositories. By default, SSH always tries to use the first key it finds, which can lead to permission issues. But don’t worry! In this guide, we’ll set up multiple SSH keys and configure them properly, so you can seamlessly work with multiple accounts on both platforms.

Step 1: Generate SSH Keys for Each Account

If you haven’t already generated SSH keys for each account, you can create them using:

ssh-keygen -t ed25519 -C "your_first_account@example.com" -f ~/.ssh/id_ed25519_first ssh-keygen -t ed25519 -C "your_second_account@example.com" -f ~/.ssh/id_ed25519_second 
Enter fullscreen mode Exit fullscreen mode

This will create two key pairs:

  • ~/.ssh/id_ed25519_first (private key) and ~/.ssh/id_ed25519_first.pub (public key)
  • ~/.ssh/id_ed25519_second (private key) and ~/.ssh/id_ed25519_second.pub (public key)

Step 2: Add SSH Keys to Git Hosting Platforms

For GitHub:

  1. Copy each public key to your clipboard:
 cat ~/.ssh/id_ed25519_first.pub cat ~/.ssh/id_ed25519_second.pub 
Enter fullscreen mode Exit fullscreen mode
  1. Go to GitHub → Settings → SSH and GPG Keys
  2. Add each key to the respective GitHub account.

For GitLab:

  1. Copy each public key:
 cat ~/.ssh/id_ed25519_first.pub cat ~/.ssh/id_ed25519_second.pub 
Enter fullscreen mode Exit fullscreen mode
  1. Go to GitLab → Settings → SSH Keys
  2. Add each key to the respective GitLab account.

Step 3: Configure SSH to Use Different Keys

By default, SSH uses git@github.com and git@gitlab.com for all connections, but we can assign different identities using an SSH configuration file.

Edit (or create) the SSH config file:

nano ~/.ssh/config 
Enter fullscreen mode Exit fullscreen mode

Then, add the following lines:

Host github-first HostName github.com User git IdentityFile ~/.ssh/id_ed25519_first IdentitiesOnly yes Host github-second HostName github.com User git IdentityFile ~/.ssh/id_ed25519_second IdentitiesOnly yes Host gitlab-first HostName gitlab.com User git IdentityFile ~/.ssh/id_ed25519_first IdentitiesOnly yes Host gitlab-second HostName gitlab.com User git IdentityFile ~/.ssh/id_ed25519_second IdentitiesOnly yes 
Enter fullscreen mode Exit fullscreen mode

This tells SSH to use different keys based on the hostname alias.

Step 4: Clone Repositories Using the Correct SSH Key

Instead of using git@github.com or git@gitlab.com, use the custom host from our SSH config:

For GitHub:

git clone github-first:your-first-user/your-repo.git 
Enter fullscreen mode Exit fullscreen mode

Or for the second account:

git clone github-second:your-second-user/your-repo.git 
Enter fullscreen mode Exit fullscreen mode

For GitLab:

git clone gitlab-first:your-first-user/your-repo.git 
Enter fullscreen mode Exit fullscreen mode

Or for the second account:

git clone gitlab-second:your-second-user/your-repo.git 
Enter fullscreen mode Exit fullscreen mode

Step 5: Update an Existing Repository’s Remote

If you already cloned a repo with the wrong account, update the remote URL:

git remote set-url origin github-second:your-second-user/your-repo.git 
Enter fullscreen mode Exit fullscreen mode

Or for GitLab:

git remote set-url origin gitlab-second:your-second-user/your-repo.git 
Enter fullscreen mode Exit fullscreen mode

Step 6: Test Your SSH Connection

To verify which account you’re using, test with:

ssh -T github-first ssh -T github-second ssh -T gitlab-first ssh -T gitlab-second 
Enter fullscreen mode Exit fullscreen mode

Each command should return a message identifying the correct user.

Final Thoughts

By configuring your SSH setup correctly, you avoid the headaches of permission issues and seamlessly switch between multiple GitHub and GitLab accounts. This method also works for Bitbucket or any other Git service that relies on SSH authentication.

Now you’re ready to juggle multiple Git accounts like a pro!

Top comments (0)