@[Toc]
In our daily development, we may need to use multiple accounts. The company develops GitLab, foreign open source Github and domestic open source Gitee. In this multi-environment scenario, we need to generate and configure multiple SSH-keys.
This article is based on the Win10 operating system, you need to install Git: Windows environment Git installation and configuration
1. Clear git global configuration
If you have configured the global username and mailbox before:
$ git config --global user.name "test"
$ git config --global user.email test@qq.com
Copy the code
Git config –global –list git config –global –list
Since it is for different development scenarios, the submission of our different environment may be different user names, so we first clear these two configurations.
$ git config --global --unset user.name "test"
$ git config --global --unset user.email test@qq.com
Copy the code
After clear global configuration, submit code also need our username, how to do?
Configure it in our working directory (project) :
$ git config user.name 'test'
$ git config user.email test@qq.com
Copy the code
2, SSH keys
Open Git bash.
- 2.1. Generate an SSH key for Github
$ ssh-keygen -t rsa -C 'Github email number' -f ~/.ssh/id_rsa_github
Copy the code
- 2.2. Generate the SSH key for Gitee
$ ssh-keygen -t rsa -C 'Gitee Email No.' -f ~/.ssh/id_rsa_gitee
Copy the code
The corresponding file is generated in the Windos User Home directory (C:\Users\ username \.ssh).
- 2.3. Log in separately
gitee
,github
addSSH KEY
Here’s gitee as an example. Github is similar to Gitee and won’t be shown here.
– Go to Settings –> SSH Public Key –> Add public Key:
– Fill in the key id_rsa_giee.pub that we generated
3. Configure config
- Open git bash in git
.ssh
To create and edit the config file, run the following command:
$ cd ~/.ssh
$ touch config
$ vim config
Copy the code
- The input
i
To enter the edit mode, enter the following information
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitee
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
Copy the code
-ESC To enter the command mode, enter :wq to save the configuration and exit.
4. Run the SSH command
Use SSH to test separately:
$ ssh -T [email protected]
$ ssh -T [email protected]
Copy the code
The configuration is successful if the following information is displayed:
$ ssh -T [email protected]
The authenticity of host 'gitee.com (212.64.62.183)' can't be established. ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'Gitee.com, 212.64.62.183' (ECDSA) to the list of known hosts.
Hi fighter3! You've successfully authenticated, but GITEE.COM does not provide shell access.
Copy the code
6, use,
- We can get through
ssh
Path to clone the project, orgit init
Then add the remote repository:
- Above is the path to a Gitee repository where we can add github’s remote repository to the project
$ git remote add github [email protected]:fighter3/dairly-learn.git
Copy the code
- And then we can put the code
push
To making
$ git push github master
Copy the code
Reference:
[1] : Configure the coexistence of Gitlab, Github and Gitee(code cloud) development environment [2] : configure multiple SSH-keys with Git