demand

The company’s computer git is GitLab, but if you want to upload something to Github, you need to use Both GitLab and Github.

To solve

First, create an SSH Key

Viewing an Existing key

In Mac, run ls ~/. SSH /. If gitlab has been configured and id_rsa and ID_rsa_pub are found, a pair of keys exist.

ls ~/.ssh/
Copy the code

A new public key should be generated and prepared for github to use

To ensure that it is different from the existing key file, we named it id_rsa_HY this time, and the last parameter is the mailbox registered with Github

ssh-keygen -t rsa -f ~/.ssh/id_rsa_hy -C "[email protected]"
Copy the code

I’m just going to press enter

SSH: ~ /.ssh: ~ /.ssh: ~ /.ssh

At this point, copy the contents of 'id_rsa_hy.pub' and create an SSH Key on Github


Different hosts correspond to the same HostName but have different keys

In the.ssh folder, create a config file and edit it so that different hosts are actually mapped to the same HostName, but the key files are different.

cd ~
cd .ssh
touch config
vim config
Copy the code

Press I to enter edit mode and enter the following code

Host github.com HostName github.com User [email protected] // User The following address is github's IdentityFile ~/.ssh/id_rsa_hy Host gitlab.xxx.cn //host is gitlab domain name HostName gitlab.xxx.cn // same as above, gitlab domain name User [email protected] // User is gitlab email IdentityFile ~/.ssh/id_rsaCopy the code

Testing SSH Connections

ssh -T [email protected]
ssh -t [email protected]
Copy the code

The message “success” means the connection is successful!

There may be:

The authenticity of host 'github.com (192.30.255.112)' can 't be established. The XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX This key is not known by any other names Are you sure you want to continue connecting (yes/no/[fingerprint])?Copy the code

Just type yes at the end

Are you sure you want to continue connecting (yes/no/[fingerprint])? // Type yes and press EnterCopy the code

Finally, when pushed/pulled, it can be pushed to the appropriate repository

Since personal GitLab is used more, the Global configuration is configured as GitLab

When you create your own warehouse, use the account configured locally

$git config --global user. Name 'gitlab '$git config --global user. Email 'gitlab' $git config --global user --local user.name 'github '$git config --local user.email 'github'Copy the code