Background#
When using git on Windows, I prefer to use SSH for pulling and pushing code, as other methods are not as cumbersome. Why use SSH? I don't want to repeatedly enter passwords when switching between different accounts. GitHub does not support direct password input and recommends using SSH. GitLab, Yunxiao, and others support connecting to remote repositories using access tokens.
SSH Configuration#
Configure SSH config file#
Create a config file in the C:\Users\catcat\.ssh directory and freely add the necessary configurations.
Here, catcat represents the username corresponding to the Windows computer; each computer's username is set by the user, so modify it accordingly.
# GitHub
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519_github_mytest
User mytest
# Github2
Host imtest.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519_github_imtest
User imtest
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519_gitee_mytest
User mytest
# gitee
Host imtest.gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519_gitee_imtest
User imtest
# gitee
Host gitcode.net
HostName gitcode.net
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_ed25519_gitcode_nxgtest
User test
Why configure two GitHub accounts? I want to separate the public keys for the two accounts. Although one public key can be shared, I still prefer to keep them separate.
Generate Key#
Execute in the directory:
Location: C:\Users\catcat\.ssh
ssh-keygen -t ed25519 -C "<comment>"
It is recommended to use the ed25519 algorithm to generate the key.
Generation result:
catcat .ssh 0ms⠀ ssh-keygen -t ed25519 -C "[email protected]" pwsh 96 15:48:07
Generating public/private ed25519 key pair.
Enter file in which to save the key (C:\Users\catcat/.ssh/id_ed25519):
Enter a custom name and press Enter.
catcat .ssh 0ms⠀ ssh-keygen -t ed25519 -C "[email protected]" pwsh 96 15:48:07
Generating public/private ed25519 key pair.
Enter file in which to save the key (C:\Users\catcat/.ssh/id_ed25519): id_ed25519_github_mytest
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_ed25519_github_mytest
Your public key has been saved in id_ed25519_github_mytest.pub
The key fingerprint is:
SHA256:FREWfhgLoEWqn21ewQWE65s+MGFoamjj0CWQvHG124 [email protected]
The key's randomart image is:
+--[ED25519 256]--+
|+. .. .+ |
|o. . . ..o+ |
| .. . oo+ |
|. . . .+ . |
|.o+ o o S . |
|+o *o* |
|+ =**.. |
|=o+++o. |
|B*o.+E. |
+----[SHA256]-----+
Configure Remote Repository SSH Key#
Copy and paste the key from the id_ed25519_github_mytest.pub file into the SSH Key section; specific details will not be elaborated here.
Configure Global Git Commit Email and User Information#
Add a .gitconfig file in the C:\Users\catcat directory and add the following configuration:
[user]
email = [email protected]
name = mytest
Attachment#
For detailed operations, see here Configure SSH Key