Loading

Git多账号配置

在一台电脑上配置多个不同的 ssh key

前言

如果拥有多个Git远程仓库,尤其是其中一个是工作中使用的仓库,只使用一个ssh key安全性很低,建议为不同Git远程仓库配置不同的ssh key。

预操作

非必须,但建议

  • 取消git全局设置

    git config --global --unset user.name    
    git config --global --unset user.email 
    
  • 为每个项目单独配置 user.name 和 user.email

    git config user.name "yourname"
    git config user.email "youremail"
    

    已经在本地存在的项目,只需再配置一下 user.name 和 user.email 即可,不需要额外的操作。

    注意 :这里配置的 name 和 email 要针对具体项目来配置,对应不同远程仓库的项目 name 和 email 可能并不一致。

SSH配置

  • 第一步 生成ssh key

    ssh-keygen -t rsa -f ~/.ssh/filename -C xxx@gmail.com  
    

    ref : http://www.cnblogs.com/popfisher/p/5731232.html

    这里有两个参数, filenamexxx@gmail.com ,前者是生成ssh密钥文件的文件名,不同远程仓库取不一样的名字,比如 id_rsa_workid_rsa_githubid_rsa_gitee 等,后者是与远程仓库地址对应的邮箱,每个也可能不一样。

    按照以上命令,生成多个ssh的密钥,并把对应的公钥(如 id_rsa_github.pub 文件里面key)添加到相应远端。

  • 第二步 添加config文件

    在.ssh目录下,新建 config 文件(文件名就是 config),设定不同git服务器的key。

    第一项是默认项。这里重点是配置 Host 字段和 IdentityFile 字段,前者对应git服务器域名,后者为对应的ssh密钥文件。

    # github
    Host github.com
    HostName github.com
    User github
    IdentityFile ~/.ssh/id_rsa_github
    
    # gitee 开源中国
    Host gitee.com
    HostName gitee.com
    User gitee
    IdentityFile ~/.ssh/id_rsa_gitee
    

    设置完成之后,可以用 ssh -T git@github.com 命令检查配置是否成功。

问题

检查是否使用的是 powershell 等 windows 的命令行工具

参考文献

原文链接:
https://www.cnblogs.com/jasongrass/p/11162918.html

posted @ 2019-07-10 11:40  J.晒太阳的猫  阅读(304)  评论(4编辑  收藏  举报