Pre

generate id_rsa file

ssh-keygen -t rsa -C "xxx1@xx.com"  # Account 1 for github

ssh-keygen -t rsa -C "xxx2@xx.com" # Account 2 for github

ssh-keygen -t rsa -C "xxx2@xx.com" # Account 3 for gitlab

Step1: Switch to ~/.ssh path

Step2: Create config file

$ touch config
$ vim config

Step3: Configure config file

# github - Account 1
Host first.github.com # reset
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

# github - Account 2
Host second.github.com # reset
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

# gitlab - Account 3
Host gitlab.com # no need to reset
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_id_rsa

How to Use

Use git clone as example:

git clone git@first.github.com:xxx.git
git clone git@second.github.com:xxx.git