When there are multiple git accounts, such as a github, used for some of your own development activities, and then a gitlab, generally the company’s internal git. these two your email address if different, there will be a problem involved, the generation of the second git key will overwrite the first key, resulting in the inevitable one can not be used.

Solution

We can configure the problem by creating a new config file in the ~/.ssh directory

Steps

1. Generate ssh key

  1. Generate the first ssh key (here I used the company email)

    Go to C:\Users\username.ssh, right click - select Git bash here, this step is very important, otherwise when you enter the key name below, the key will be generated in the path of the currently open git bash, for example, I opened git bash on the desktop, it will be generated on the desktop:.

    1
    
    ssh-keygen -t rsa -C "yourmail@example.com"
    

    When you enter the file name and path, the generated key is named id_rsa_gitlab and the following successive carriage returns. (Whether to set a password for the key depends on your needs)

    1
    2
    3
    4
    5
    6
    7
    
    $ ssh-keygen -t rsa -C "yourmail@example.com"
    Generating public/private rsa key pair.
    Enter file in which to save the key (/c/Users/李锋镝/.ssh/id_rsa): id_rsa_gitlab
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in id_rsa_gitlab
    Your public key has been saved in id_rsa_gitlab.pub
    
  2. Generate a second ssh key (using my GitHub email here), same steps as above

    1
    
    ssh-keygen -t rsa -C "yourmail@qq.com"
    

    The key generated here is named id_rsa_githab.

    1
    2
    3
    4
    5
    6
    7
    
    $ ssh-keygen -t rsa -C "yourmail@qq.com"
    Generating public/private rsa key pair.
    Enter file in which to save the key (/c/Users/李锋镝/.ssh/id_rsa): id_rsa_githab
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in id_rsa_githab
    Your public key has been saved in id_rsa_githab.pub
    

2. Execute the ssh-agent command

ssh-agent is a key manager. After running ssh-agent, use ssh-add to give the private key to ssh-agent for safekeeping, and other applications can give the authentication application to ssh-agent to complete the whole authentication process when they need to authenticate.

1
ssh-agent

Or execute the following command line.

1
2
exec ssh-agent bash
eval ssh-agent -s

3. To add the private key, execute the following command

1
2
ssh-add ~/.ssh/id_rsa_github
ssh-add

4. Create and modify the config file

Create a config file and place it in the .ssh directory:

Two ways.

  1. Create a new txt text under windows and change the name to config (including the .txt suffix)
  2. Under git bash, directly touch config to create a config file

The configuration is as follows.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# gitlab
Host gitlab.example.com
HostName gitlab.example.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_kuaidao
User lifengdi

# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
User lifengdi

Configuration file description.

Each account is configured with a separate Host, and each Host should take an alias. Each Host can be configured with two main properties, HostName and IdentityFile.

The name of the Host can be taken as your favorite name, but this will affect git-related commands.

For example.

1
2
3
4
5
6
7
8
Host mygithub If you define it this way, the command would be as follows, i.e. the name immediately after git@ would be changed to mygithub
git clone git@mygithub:lifengdi/example.git
It is equivalent to the real domain name of your configured HostName, mapped to the name of the configuration behind the Host

HostName                      #This is the real domain address
IdentityFile                  #Here is the address of id_rsa
PreferredAuthentications      #Configure what permission authentication to use when logging in - can set publickey, password publickey, keyboard-interactive, etc.
User                          #Configure the username to use

5. Add the public key to gitlab or github

The one at the end of .pub is the public key, which can be configured to SSH keys on git.

Test if the key is valid

Enter the following code.

1
2
3
4
5
# github
ssh -T git@github.com

# gitlab
ssh -T git@gitlab.example.com

Show.

1
2
3
4
5
# github (successfully means that is successful, if there are warnings and so on, ignore it)
Hi lifengdi! You've successfully authenticated, but GitHub does not provide shell access.

# gitlab
Welcome to GitLab, lifengdi!