Setting up Git for several Github or Gitlab accounts

gealber

Gealber Morales

Posted on June 12, 2022

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:

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.

Setting SSH

First of all, let's create a ssh key-pair for each of these accounts. Take a look at the documentation for do so in GitHub
or GitLab, both documentation are quite good.

I will assume you created several ssh keys, let's named them as:

  • github_personal
  • gitlab_personal
  • gitlab_company

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

Enter fullscreen mode Exit fullscreen mode

Add ssh key to account in platform

In order to add these keys on GitHub follow this documentation. While for GitLab we have the following documenation.

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 git@github.com
Enter fullscreen mode Exit fullscreen mode

Personal account GitLab

ssh -T git@gitlab.com
Enter fullscreen mode Exit fullscreen mode

Company account GitLab

ssh -T git@gitlab.funnycompany.com
Enter fullscreen mode Exit fullscreen mode

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.

In ~/.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://git@gitlab.com/"]
    insteadOf = https://gitlab.com/

[url "ssh://git@github.com/"]
    insteadOf = https://github.com/

[user]
    email = gealber@email.com
    name = Gealber
Enter fullscreen mode Exit fullscreen mode

In the first lines, we specified that every time we make git operations from folder ~/Work/FunnyCompany we are going to use .gitconfig-funnycompany
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 = gealber@funnycompany.com
name = gealber

[gitlab]
user = "gealber"

[url "ssh://git@gitlab.funnycompany.com/"]
    insteadOf = https://gitlab.funnycompany.com/

Enter fullscreen mode Exit fullscreen mode

After this setup we could make git operations without problem.

💖 💪 🙅 🚩
gealber
Gealber Morales

Posted on June 12, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related