Skip to content

Instantly share code, notes, and snippets.

@ayrokid
Forked from dotspencer/multiple-keys-gitlab.md
Created March 1, 2021 23:23
Show Gist options
  • Save ayrokid/b3fbe6f14e614fd050a4bdcdc393f9e1 to your computer and use it in GitHub Desktop.
Save ayrokid/b3fbe6f14e614fd050a4bdcdc393f9e1 to your computer and use it in GitHub Desktop.
Multiple Gitlab accounts with multiple ssh keys

Gitlab won't allow reuse of a public ssh key for multiple accounts. To get around this you need to create a second ssh key for the second account.

Create or modify your ~/.ssh/config file:

# normal                                                                                                                                                                  
Host gitlab.com-work_username
     HostName gitlab.com
     PreferredAuthentications publickey
     IdentityFile ~/.ssh/id_rsa

# second                                                                                                                                                                  
Host gitlab.com-personal_username
     HostName gitlab.com
     PreferredAuthentications publickey
     IdentityFile ~/.ssh/second_key

Check that both authenticate correctly with Gitlab:

ssh -T [email protected]_username
ssh -T [email protected]_username

Then in the repo you want to use the second account for, edit the .git/config file:

[remote "origin"]
        url = [email protected]_username:group-name/repo-name.git

Added the -personal_username after gitlab.com


To clone the repo initially you can use:

GIT_SSH_COMMAND='ssh -i ~/.ssh/<second_private_key> -o IdentitiesOnly=yes' git clone <repo_url.git>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment