In some cases, you may need to use multiple Git accounts on one computer, and you may need to set up different Settings for multiple platforms and accounts.

Train of thought

Manage multiple SSH keys simultaneously.

The solution

Generate multiple SSH keys

Here two accounts are used for example

Ensure that multiple SSH keys are generated in the ~/. SSH directory. Otherwise, the generated sshkeys will not be generated in the ~/.

The following operations are performed in the ~/. SSH directory.

Try to delete all files in this directory before generation to avoid unnecessary problems.

ssh-keygen -t rsa -C "[email protected]"
Copy the code
ssh-keygen -t rsa -C "[email protected]"
Copy the code

Rename SSH files (id_rsa_one and ID_rsa_two) the first time you type the command line and ask Enter file in which to save the key. This generates four files in the following directory.

Two files containing four private and public keys.

Add the private key

Add the private key generated by both accounts to the platform where the private key is added. Access to the private key

cat ~/.ssh/id_rsa_one.pub
Copy the code
cat ~/.ssh/id_rsa_two.pub
Copy the code

Pub and id_rsa_two-. pub are the names of the previously renamed SSH files

Create config file

Create a config file in the ~/.ssh directory

touch config
Copy the code

An empty config file is generated in the ~/.ssh directory, to which we add the following.

#git server one
Host one.aliyun.com # alias
Hostname code.aliyun.com # Real domain name
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_one SSH file path
User one
#git server two
Host two.aliyun.com
Hostname code.aliyun.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_two
User two
Copy the code

Remote test [skip]

SSH - T one.aliyun.comCopy the code
SSH - T two.aliyun.comCopy the code

use

Clone to the local

The old way of writing it

[email protected]: indicates the project path. GitCopy the code

The way we write it now

  git clone[email protected]: indicates the project path. GitCopy the code
  git clone[email protected]: indicates the project path. GitCopy the code

Set local user name and mailbox for warehouse [optional]

  git config user.name "one_name" ; git config user.email "one_email"
  git config user.name "two_name" ; git config user.email "two_email"
Copy the code

summary

This article is through their own online inquiry and then their own practice summary. Due to my limited knowledge, it is inevitable that the summary is very complete. If the readers encounter any problems, welcome to leave a message to discuss and learn together.