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
-
Generate the first ssh key (here I used the company email)
Go to
C:\Users\username.ssh
, right click - selectGit 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 opengit bash
, for example, I openedgit 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
-
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.
|
|
Or execute the following command line.
3. To add the private key, execute the following command
4. Create and modify the config file
Create a config
file and place it in the .ssh
directory:
Two ways.
- Create a new txt text under windows and change the name to config (including the .txt suffix)
- Under git bash, directly touch config to create a config file
The configuration is as follows.
|
|
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.
|
|
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.
Show.