Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 12, 2022 08:09 am GMT

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

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

Personal account GitLab

Company account GitLab

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://[email protected]/"]    insteadOf = https://gitlab.com/[url "ssh://[email protected]/"]    insteadOf = https://github.com/[user]    email = [email protected]    name = Gealber

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 = [email protected] = gealber[gitlab]user = "gealber"[url "ssh://[email protected]/"]    insteadOf = https://gitlab.funnycompany.com/

After this setup we could make git operations without problem.


Original Link: https://dev.to/gealber/setting-up-git-for-several-github-or-gitlab-accounts-6io

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To