window下配置多个git账号

前言:工作中使用公司git账号,自己写的小demo用个人git账号

参考:https://www.cnblogs.com/popfisher/p/5731232.html

1. 生成github.com对应的私钥公钥(本文中文件地址C:\Users\popfisher目录)

执行命令ssh-keygen -t rsa -C 774*****[email protected] 创建github对应的sshkey,命名为id_rsa_github,密码123456 

代码:ssh-keygen-t rsa -C 774*****[email protected]

注:在git bash 里输入上面代码回车,第一行是指定文件名称,默认 id_rsa , 在配置多个账号时,应修改名称,避免覆盖之前已配置好的账号。

生成的私钥和公钥文本最好保存在之前保存的 同一文件夹下,一般保存在 c/Users/admin/.ssh 下


2.在.ssh目录创建config文本文件并完成相关配置(最核心的地方)

每个账号单独配置一个Host,每个Host要取一个别名,每个Host主要配置HostNameIdentityFile两个属性即可

Host的名字可以取为自己喜欢的名字,不过这个会影响git相关命令,例如:

Host github 这样定义的话,命令如下,即git@后面紧跟的名字改为github 

git clone git@mygithub:PopFisher/AndroidRotateAnim.git


HostName 这个是真实的域名地址

IdentityFile 这里是id_rsa的地址

PreferredAuthentications配置登录时用什么权限认证--可设为publickey,password publickey,keyboard-interactive等

User 配置使用用户名

易遗漏: 1. 把公钥添加到你想添加的托管平台 ;2、平台绑定生成公钥的邮箱;3、后面clone 的时候使用 ssh地址

3.打开Git Bash客户端(管理员身份运行)执行测试命令测试是否配置成功(会自动在.ssh目录生成known_hosts文件把私钥配置进去)

代码: ssh -T git@github ----> github 为config 中配置的 host名字

返回:Hi ************ ! You've successfully authenticated, but GitHub does not provide shell access.  则连接成功


此时,基本已配置好。

经测试: 一个邮箱生成的 公钥私钥 可用在不同托管平台多次使用;如:

Host me

    HostName gitee.com

    PreferredAuthentications publickey

    IdentityFile ~/.ssh/id_rsa_me

Host github

    HostName github.com

    PreferredAuthentications publickey

    IdentityFile ~/.ssh/id_rsa_me

你可能感兴趣的:(window下配置多个git账号)