Oct 10, 2023

                                               [Runyi Yang](<https://runyiyang.github.io/>) / [Runyi’s Blogs](<https://runyiyang.notion.site/Runyi-s-Blogs-f52d6bf73e104c51a4f5e80529b6a9b6>)

Why Do I Need To Set Up Both?

At work, sometimes multiple git accounts are used on the same computer, such as company GitLab account with personal GitHub account.

How to use GitLab and GitHub, switch to the corresponding account, and no passcode? In this case, we need to use ssh (git can choose to use https and ssh for communication, but when using https, you need to enter the account and password each time fetch and push code).

Take personal GitHub account and company GitLab account as a case, the following demonstrates how. It is successfully tested on MacOS M2 Max devices, tested on the Imperial GitLab and general GitLab pages.

Setup Github and Gitlab SSH Key

1. Create SSH Keys for Both Accounts

First, you'll need to create separate SSH keys for both accounts.

# For GitHub
$ ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_github

# For GitLab
$ ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_gitlab

# For Imperial
$ ssh-keygen -t rsa -b 4096 -C "[email protected]" -f ~/.ssh/id_rsa_ic

2. Add SSH Keys to the Corresponding Accounts

For each key:

3. Create a SSH Config File

Edit (or create) the SSH config file:

$ vim ~/.ssh/config

Add the following:

# GitHub Account
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_github

# GitLab Account
Host gitlab.com
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_rsa_gitlab

# (Options) For Imperial Account
Host imperial
User {Username}
Hostname gitlab.doc.ic.ac.uk
IdentityFile ~/.ssh/id_rsa_ic

This configuration tells SSH which key to use for which domain.

4. Clone and Set Repositories