Managing git ssh-keys for multiple accounts on GitLab and GitHub
⌗
If you only have one account for every git repository provider, for example, one
for www.github.com
and one for www.gitlab.com
, you will need to add the
following to your ssh-config file, ~/.ssh/config
host github.com
HostName github.com
IdentityFile ~/.ssh/github
User git
host gitlab.com
HostName gitlab.com
IdentityFile ~/.ssh/gitlab
User git
where the identity files contain your private key for each account. These can be generated by running
ssh-keygen
which will prompt you to specify a location and a passphrase:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/rsoko/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
these are all optional, but to work with the above config we need to generate
two keys, one located at ~/.ssh/gitlab
and one at ~/.ssh/github
. As for the
passphrase, this can be left empty if you don’t want to enter a password every
time you create an ssh connection (for example when pushing code to the remote
repository).
check if everything works:
ssh -T git@gitlab.com
which gives me
Welcome to GitLab, @rsokolewicz!
multiple accounts per provider⌗
If you have multiple accounts on for example gitlab.com (e.g. one personal and one work), you can configure the ssh-config as follows
host gitlab-private
HostName gitlab.com
IdentityFile ~/.ssh/gitlab_private
User git
host gitlab-work
HostName gitlab.com
IdentityFile ~/.ssh/gitlab_work
User git
and follow the steps as before to generate gitlab_private
and gitlab_work
.
The crucial step now is when setting up the remote-url
of a git repository to
map it to the correct entry in the above ssh configuration:
git remote set-url origin git@gitlab-private:repo.git
git@gitlab-private:repo.git
has two important parts: git@...:
and
:....git
. The first part that’s between git@...:
corresponds to the name of
the host that is specified in the ssh configuration. In the above case host gitlab-private
. The name can be anything and is there just to map the correct ssh key to the corresponding repo. The second part :...git
is the git-url to the repo.