This is the second day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Github or gitLab uses SSH to connect to the client. Here I take gitLab as an example. By default, when adding a public Key to the second gitLab account (on the same computer), it will prompt us that the current public Key has been used by others, please regenerate it (SSH Key cannot be added repeatedly).

Create an SSH key

Execute command:

ssh-keygen -t rsa -f /c/Users/Administrator/.ssh/id_rsa_second -C "[email protected]" 
Copy the code

SSH directory: id_rsa_second and id_rsa_second.pub. Similarly, we add the newly generated public key to the new gitLab account.

2: Add the key to the SSH Agent

Execute command:

ssh-agent bash
ssh-add /c/Users/Administrator/.ssh/id_rsa_second
Copy the code
3. Configure the Config file

By default, there is no config file in the.ssh directory. We manually create a config file in the.ssh directory (no suffix required). Then open the config file with the editor, and configure a Host for the new account. The Host should be an alias, and each Host should be configured with HostName and IdentityFile. As follows:

Host gitLab-second
HostName gitLab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_second
Copy the code

Check whether the configuration is successful:

ssh -T git@gitLab-second
Copy the code

If your nickname is displayed, the configuration is successful. For example, Hi (you name)! You’ve successfully authenticated, but GITEE.COM does not provide shell access. After the configuration is complete, when connecting to the gitLab repository that is not the default account, the address of the remote repository should be changed. For example, now clone a repository under the second account XXXXX:

git clone git@gitLab-second:name/xxxxx.git
Copy the code

You can actually use it now, but there are other configurations on the web

4. Cancel the global user name and mailbox configuration

Because multiple Git accounts are configured on one computer, it is no longer possible to configure the global user name and mailbox, but to configure the corresponding local user name and mailbox in different repositories. Open git bash and run the following command to cancel the global configuration:

git config --global --unset user.name
git config --global --unset user.email
Copy the code
Configure a user name and mailbox for a repository

Go to a repository root directory, open git bash, and enter the following command to configure a local user name and mailbox for it:

Git config user.name "xx" git config user.email "[email protected]" git config --listCopy the code

Finished!