windows配置多账户git
· 阅读需 3 分钟
下载安装git
下载地址Git - Downloads
配置
随便一个目录(桌面就行),鼠标右键,选 Open Git Bash here
配置第一个git平台
我们配置github
- 生成SSH KEY:
执行命令:ssh-keygen -t ed25519 -C "github SSH Key" -f ~/.ssh/github_hello
参数:
-t
key类型(可选择ed25519
(建议)或rsa
,区别)-C
注释-f
文件生成位置(建议根据不同平台/不同账号使用不同的文件名)
生成中间通过3次回车,最终生成SSH密钥对。
-
查看生成的密钥对
生成一个私钥一个公钥。私钥保密,公钥需要设置到git平台上
打开目录:C:\Users\{用户名}\.ssh
生成的私钥文件:github_hello
,公钥文件:github_hello.pub
- 将公钥设置到git平台上。
在git平台上,一般在 个人设置中心 - ssh公钥 处。将公钥文件打开,把原文复制到平台上。
- 测试连通性
执行命令:ssh -T git@github.com
正常情况下第一次需要输入yes,最终连接上会返回用户名即可。
配置第二个平台
先熟悉第一个账号的配置过程。
接下来我们配置gitee
- 1、2、3操作一遍,这时候默认你创建了
gitee_hello
和gitee_hello.pub
- 打开
.ssh
文件夹,创建文件config
,输入以下内容:
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_hello
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_hello
配置项:
- Host:git 服务器的域名(本地连接时的地址,可以认为是别名)
- HostName:git 服务器的域名
- IdentityFile:指定私钥的路径
- 执行第4步测试连通性
执行命令:ssh -T git@gitee.com
返回用户名即可。
同一git平台有多个账号
我和其他人在这台电脑上都用gitee。
-
1、2、3操作一遍,这时候默认你又创建了
gitee_world
和gitee_world.pub
-
打开
.ssh
文件夹,修改文件config
:
🪄 注意高亮的Host配置
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_hello
# gitee(hello)
Host gitee_hello.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_hello
# gitee(world)
Host gitee_world.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_world
注意高亮处,使用Host进行区分,以hello账号拉取推送时,使用Host:gitee_hello.com,以world账号拉取推送时,使用Host:gitee_world.com。
- 执行第4步测试连通性
执行命令:ssh -T git@gitee_hello.com
执行命令:ssh -T git@gitee_world.com
分别返回用户名即可。
提示
当Host和HostName不同时,需要注意的是,平台上复制是默认的域名,你得自己改成自己的对应的Host。