开发过程中可能会需要在同一台电脑上配置两个git账号,一个账号用来将公司产品推到公司的代码仓库,另一个账号需要将自己私人的“玩具”推到github等平台,这就可能会需要使用不同的端口号或者不同的rsa秘钥(对应不同的邮箱地址)连接不同的代码仓库。
具体方法
打开“Git Bash”命令行工具
Git Bash
输入命令
其中-C后面引号中内容换成自己的邮箱,-f后面是新的公钥秘钥路径
ssh-keygen -t rsa -C "xxxxxx@qq.com" -f .ssh/newfile
中间按几次回车,执行结果如下图
执行结果
在当前用户文件夹的“.ssh”文件夹中可以找到生成的公钥秘钥文件
ssh秘钥文件
将公钥配置到远程仓库的ssh配置中
在.ssh文件夹新建“config”文件,使用文本格式打开
config文件内容
- “#”开头的是注释
- Host 后面是名称,可以随便写,每一个Host就代表一个仓库,仓库参数另起一行,前有4个空格
- 参数 - HostName : ssh的host,SSH地址“git@”后面到冒号前面的内容
- 参数 - User : 使用“git”
- 参数 - PreferredAuthentications : 使用“publickey”即可,git仓库均为这种形式
- 参数 - IdentityFile : 该host后台配置的公钥对应的私钥地址的绝对路径
- 参数 - Port : SSH默认端口号为22,某些私有部署的git仓库会更换端口号
SSH HostName
#gitee server one
Host 阿里云code
HostName code.aliyun.com
User git
PreferredAuthentications publickey
IdentityFile C:\Users\1xxxx\.ssh\id_rsa
#gitee server two
Host github
HostName github.com
User git
PreferredAuthentications publickey
IdentityFile C:\Users\1xxxx\.ssh\id_rsa_1xxxxx
#gitee server three
Host coding
HostName e.coding.net
User git
PreferredAuthentications publickey
IdentityFile C:\Users\1xxxx\.ssh\id_rsa_1xxxxx
#gitee server four
Host 公司仓库
HostName rd.xxxxxx.com
Port 10xxx
User git
PreferredAuthentications publickey
IdentityFile C:\Users\1xxxx\.ssh\id_rsa
将config文件保存即可完成所有配置