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.
We can configure the problem by creating a new
config file in the
1. Generate ssh key
Generate the first ssh key (here I used the company email)
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 bashon the desktop, it will be generated on the desktop:.
ssh-keygen -t rsa -C "email@example.com"
When you enter the file name and path, the generated key is named
id_rsa_gitlaband the following successive carriage returns. (Whether to set a password for the key depends on your needs)
$ ssh-keygen -t rsa -C "firstname.lastname@example.org" 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
ssh-keygen -t rsa -C "email@example.com"
The key generated here is named
$ ssh-keygen -t rsa -C "firstname.lastname@example.org" 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-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
config file and place it in the
- 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.
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.