Windows下Git多账号配置,同一电脑多个ssh-key的管理,适用于GitHub账号和其它类似服务器账号

需求:

公司有github账号,自己有github账号,想在git上同时使用,两者互不干扰。

思路:

管理两个SHH key。

备注:这篇文章所有执行命令的地方都是在管理员模式下进行,即打开cmd,Git Bash客户端用管理员身份运行程序。


解决方案:

一、生成两个SSH key

为了举例方便,这里使用“one”和“two”两个账户。下同。

$ ssh-keygen -t rsa -C "one@gmail.com"

$ ssh-keygen -t rsa -C "two@gmail.com"

不要一路回车,分别在第一个对话的时候输入重命名(id_rsa_oneid_rsa_two),这样会生成两份包含私钥和公钥的4个文件。

注1:ssh-keygen是linux命令,可以让两个机器之间使用ssh而不需要用户名和密码

住2:一定要在~/.ssh路径下运行命令行,不然生成的文件不会出现在当前目录

二、添加私钥

1、打开ssh-agent


(1)如果你是github官方的bash:

$ ssh-agent -s

(2) 如果你是其它,比如msysgit:

$ eval $(ssh-agent -s)

2、添加私钥

$ ssh-add ~/.ssh/id_rsa_one

$ ssh-add ~/.ssh/id_rsa_two

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

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

Host的名字可以取为自己喜欢的名字,不过这个会影响git相关命令,例如:
Host mygithub 这样定义的话,命令如下,即git@后面紧跟的名字改为mygithub
git clone git@mygithub:PopFisher/AndroidRotateAnim.git

HostName           这个是真实的域名地址
IdentityFile          这里是id_rsa的地址
PreferredAuthentications   配置登录时用什么权限认证--可设为publickey,password publickey,keyboard-interactive等
User            配置使用用户名

$ touch config

此时会出现空的config文件,然后添加如下内容:

# one(one@gmail.com)

    Host one.github.com

  HostName github.com

  PreferredAuthentications publickey

  IdentityFile ~/.ssh/id_rsa_one

  User one

# two(two@ gmail.com)

    Host two.github.com

  HostName github.com

  PreferredAuthentications publickey

  IdentityFile ~/.ssh/id_rsa_two

  User two

四、部署SSH key

分别登陆两个github账号,进入Personal settings –> SSH and GPG keys

点击"new SSH key", 把下面两个公钥的内容分别添加到相应的github账号中。

  

五、远程测试【可跳过】

$ ssh –T git@one.github.com

$ ssh –T git@two.github.com

六、使用

1、clone到本地

(1)原来的写法:

$ git clone git@github.com: one的用户名/learngit.git

(2)现在的写法:

$ git clone git@one.github.com: one的用户名/learngit.git

$ git clone git@two.github.com: two的用户名/learngit.git

2、记得给这个仓库设置局部的用户名和邮箱:

$ git config user.name "one_name" ; git config user.email "one_email"

$ git config user.name "two_name" ; git config user.email "two_email"

3、上述都成功后,会发现钥匙会由灰变绿。

转载加修改

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

           https://www.cnblogs.com/xjnotxj/p/5845574.html

           https://www.jianshu.com/p/0ad3d88c51f4

原文地址:https://www.cnblogs.com/oneDongHua/p/14264086.html