To sum up the article in one sentence:

If multiple SSH-keys are required, configure the config file to resolve this problem.


Quote:

Summary about the use of Git has written an article before: xiaozhu Git use summary, to deal with the daily development of basic enough, but in the actual development, will encounter a variety of problems. For example, our company recently migrated projects from its own Gitlab server to the code cloud, and encountered a problem: multiple SSH-key management problems. Generally speaking, a single SSH-key can be used on multiple Git servers. It is only used for encrypted access, as long as the public key on the server can be paired with the local private key. However, different accounts cannot use the same public key!

If you’ve ever used Git to commit code, you’re probably familiar with SSH keys. In the Clone project, there are two protocols available: HTTPS and SSH:

The link looks like this:

https://gitee.com/xxx/YYY.git
[email protected]:xxx/YYY.git
Copy the code

You can clone any project using Https, but you need to enter the account password for Push through SSH, which can only be clone by the owner and administrator of the project. If ssh-key is configured, you do not need to enter the account password for Push (provided that the password is not set during ssh-key configuration). You can just submit it.

You can use the following simple command to create an Ssh-key

cd~ /. SSH# If the path does not exist, type the following command to create an SSH folder
mkdir ~/.ssh

Type the following command to generate the public and private keys for ssh-key
# will ask you to enter the file name, password, password, and then generate id_rsa and id_rsa.pub by default
ssh-keygen -t rsa -C "[email protected]"

You can also keep it short and put your name right after the command
ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/id_rsa_xx

This command generates the private and public keys of id_rsa_xx and id_rsa_xx.pub.
Copy the code

After creating the ssh-key, you need to open the public key, copy the contents of the ssh-key, and post it to the ssh-key configuration page of your Gitlab, Github, and code cloud.

After the configuration is successful, type:

ssh -T [email protected]
Copy the code

If the following message is displayed, the configuration is complete


The question

Git remote add Git remote add Git remote add Git remote add Git remote add Git remote

git remote add origin [email protected]
git remote add osc [email protected]
Copy the code

Then manually specify the branch of the remote repository when pushing or pulling

git push origin master
git pull osc master
Copy the code

**ssh-keygen -t rsa-c “[email protected]”** ssh-keygen -t rsa-c “[email protected]”** ssh-keygen -t rsa-c [email protected]

Solutions are as follows:

  • Step 1: Assuming that I have an account and a company account, I can use the following command to create two different sSH-keys
ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/id_rsa_my
ssh-keygen -t rsa -C "[email protected]" -f ~/.ssh/id_rsa_work
Copy the code
  • Step 2: Set the SSH key proxy
ssh-add -l

Could not open a connection to your authentication agent
Type the following command:
exec ssh-agent bash
Copy the code
  • Step 3: Add a private key
ssh-add ~/.ssh/id_rsa_my
ssh-add ~/.ssh/id_rsa_work

To view all ssh-key agents, type ssh-add-l above
Type ssh-add -d to delete all agents
Copy the code
  • Copy the contents of the public key (.pub) to your remote repository

  • Step 5: Add and edit the config file

# Bash can be created with touch, Ubuntu can be created with gedit~
vim ~/.ssh/config

# File contents
# work
Host gitee
    HostName gitee.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_work
    user git
# my
Host github
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_my
    user git
    
# config file parameters
# Host: configure the corresponding Host name and SSH file for the identified pattern
# HostName: Specifies the name of the host to be logged in to
# PreferredAuthentications: indicates the login mode, and indicates the publickey
# IdentityFile: private key full path name
# User: indicates the login name
    
Copy the code
  • Step 6: After the configuration is complete, enter the following commands to check whether the configuration takes effect
ssh -T [email protected]
ssh -T [email protected]
Copy the code

This configuration is complete ~


There is a little

By the way, some friend will submit the SSH key and code of confusion, because in the configure SSH – key commands Followed by a string of mailbox, thought it is submitted to the author email, actually otherwise, this thing is set up through the git config command! Git server configures submitter information based on user.email and user.email in the configuration file.

git config user.name "CoderPig"Git config user.email"[email protected]"/ / emailCopy the code