git多账户配置

    1.生成公钥,私钥
在Git使用中经常会碰到多用户问题,例如:你在公司里有一个git账户,在github上有一个账户,并且你想在一台电脑上同时对这两个git账户进行操作,此时就需要进行git多用户配置。
    首先配置不同的SSH KEY,使用ssh-keygen命令产生两个不同的SSH KEY,进入.ssh目录:
#切换到.ssh目录
cd ~/.ssh  
#使用自己github的注册邮箱产生SSH KEY
ssh-keygen -t rsa -C "[email protected]"  
#github的SSH KEY
Enter file in which to save the key (/Users/ltc/.ssh/id_rsa): /c/Users/Administrator/.ssh/id_rsa_github
id_rsa_github
#将ssh key添加到SSH agent中 
ssh-add ~/.ssh/id_rsa_github

实例图如下:

*注: 如果执行 ssh-add 时显示错误 Could not open a connection to your authentication agent. 那么执行

eval `ssh-agent -s`

后重新执行 ssh-add 那条命令即可。

成功提示如下:

$ ssh-add ~/.ssh/id_rsa_github
Identity added: /c/Users/Administrator/.ssh/id_rsa_github (/c/Users/Administrator/.ssh/id_rsa_github)

如图所示

2.修改config配置文件指定账户使用的公钥

Host github.com
   User git
   Hostname github.com
   IdentityFile ~/.ssh/id_rsa_github

3.在生成ssh key之后,需要分别在github的profile中编辑SSH KEY,以github为例:

在图中添加Title,可以随便写:
将.ssh目录下对应的id_rsa_github.pub中的内容拷到Key中,点击Add SSH key按钮即可。

4.测试下看看成功与否

$ ssh -T [email protected]
Hi running-fly! You've successfully authenticated, but GitHub does not provide shell access.

猜你喜欢

转载自www.cnblogs.com/zxqblogrecord/p/10123172.html