It’s quite easy if we only have one git account, then it became confusing when we want to add more account. I also met this problem. But I have found out solutions so let me show how to handle it.

You should know the difference between git and github. github is one kind of git, and git have many website name such as gitlab, gitee… So there are the following two situations. however you can consider it as other 2 situations: same email or not. P.S. Local environment will be automatically adjusted by user.name, user.email and repo url.

  • different git website accounts for example: a.github.com and b.gitlab.com
  • same git website accounts for example: a.github.com and b.github.com

SSH

In the beginning, there is a base knowledge we need to be aware of. SSH is a authorization tool. It is a bridge connecting mutiple network servers. And abviously if we want to update a git repo, a trusted connection is needed. So we need to use SSH to generate public key and private key, and copy public key to website setting page. And add basic information of website to .ssh/config. So that we are trusted and can continue doing pull or push operations.

Let’s generate ssh keys for different emails

Notice: email number = ssh key number If github and gitlab is registered by same email, then it can use same ssh key. Otherwise you are supposed to create new ssh keys and add them to website setting page accordingly.

ssh-keygen -t rsa -C "CompanyEmailNameA@xxx.com" -f ~/.ssh/company_name_rsa
ssh-keygen -t rsa -C "SelfEmailNameA@xxx.com" -f ~/.ssh/self_name_rsa

Update different ssh key’s config

Accounts of different website:

vi ~/.ssh/config

Host self_name
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/company_name_rsa
User self_github_account_name
  
Host self_name
Hostname gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/self_name_rsa
User self_gitlab_account_name

Accounts of same website:

vi ~/.ssh/config

Host company_name
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/company_name_rsa
User company_github_account_name
  
Host self_name
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/self_name_rsa
User self_github_account_name

Copy public key to website setting page

Git config

git config user.name "self_github_account_name"
git config user.email SelfEmailNameA@xxx.com

That should be enough.

Conclusion

The process from one to many is the process of deepening understanding.