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
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:
- Copy each public key to your clipboard:
cat ~/.ssh/id_ed25519_first.pub
cat ~/.ssh/id_ed25519_second.pub
- Go to GitHub → Settings → SSH and GPG Keys
- Add each key to the respective GitHub account.
For GitLab:
- Copy each public key:
cat ~/.ssh/id_ed25519_first.pub
cat ~/.ssh/id_ed25519_second.pub
- Go to GitLab → Settings → SSH Keys
- 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
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
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
Or for the second account:
git clone github-second:your-second-user/your-repo.git
For GitLab:
git clone gitlab-first:your-first-user/your-repo.git
Or for the second account:
git clone gitlab-second:your-second-user/your-repo.git
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
Or for GitLab:
git remote set-url origin gitlab-second:your-second-user/your-repo.git
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
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)