Setting up Git for several Github or Gitlab accounts
When you work as software developer, you may be in the following situation. You have your personal GitHub and Gitlab accounts, but in your new job they are using also Github or Gitlab. Due to security concerns, is very common that companies only let you use accounts with an email from this company. Let’s assume that at the end we have the following accounts:
- Personal GitHub account associated with email, email@example.com.
- Personal GitLab account also associated with email, firstname.lastname@example.org.
- Work GitLab account associated with email, email@example.com
If you don’t have such number of accounts is normal, I’m just trying to cover all possible scenarios here. So we need to access all that from my single laptop, of course, I should have access to the privates repositories in all these accounts.
I will assume you created several ssh keys, let’s named them as:
Now we need to tell ssh, which ssh key to use when we make a request to a given domain. Let’s open
~/.ssh/config file and set the necessary configs:
# For personal github account. Host github.com Hostname github.com User git IdentityFile ~/.ssh/github_personal # For personal gitlab account. Host gitlab.com Hostname gitlab.com PreferredAuthentications publickey User git IdentityFile ~/.ssh/gitlab_personal # For company account. Host gitlab.funnycompany.com Hostname gitlab.funnycompany.com PreferredAuthentications publickey User git IdentityFile ~/.ssh/gitlab_company
Add ssh key to account in platform
Checking ssh configurations
In order to check if this configuration are actually working properly, you must test it with the following command:
Personal account GitHub
ssh -T firstname.lastname@example.org
Personal account GitLab
ssh -T email@example.com
Company account GitLab
ssh -T firstname.lastname@example.org
All of this command should return you a successful message, otherwise it will hang for ever.
Telling git how to ssh
Now we need to let know git that these configurations are available, so let’s open
~/.gitconfig. Before that, I would recommend you to have
separates folders for your personal code and company code. I will assume you have the following folders: Work/FunnyCompany and Personal/AwesomeCode.
~/.gitconfig let’s have the following set up:
# Setup for Gitlab Company [includeIf "gitdir:~/Work/FunnyCompany/"] path = ~/Work/FunnyCompany/.gitconfig-funnycompany [core] excludesfile = ~/.gitignore # For personal accounts. [url "ssh://email@example.com/"] insteadOf = https://gitlab.com/ [url "ssh://firstname.lastname@example.org/"] insteadOf = https://github.com/ [user] email = email@example.com name = Gealber
In the first lines, we specified that every time we make git operations from folder
~/Work/FunnyCompany we are going to use
file in this same folder to fetch configurations. Otherwise, we just tell git to use ssh on every https request. In the
.gitconfig-funnycompany we have
[user] email = firstname.lastname@example.org name = gealber [gitlab] user = "gealber" [url "ssh://email@example.com/"] insteadOf = https://gitlab.funnycompany.com/
After this setup we could make git operations without problem.