美文网首页
一台主机中使用多个github账号

一台主机中使用多个github账号

作者: 小爨 | 来源:发表于2016-09-05 17:44 被阅读0次

程序员很可能有两个或者更多的github账号的需求,一个用于工作,一个用于自己。

使用github,那么就要设置ssh key了,否则你没法与github交互。当你将自己主机生成的ssh key上传到一个github账号之后,再把同样的key上传到另一个github账号时,会出现下面的情况:

github.png

提示你key 已经存在了。

要解决这个问题,也是很简单的:

a. 生成一个新key,注意别覆盖你之前存在的key了!!!

ssh-keygen -t rsa -C "你的邮箱地址"

生成key时,请必须指定一个新的文件名字,例如我指定的如下:

~/.ssh/id_rsa_me

b. github添加公钥

将~/.ssh/id_rsa_me.pub上传到第二个github账号中

c. 修改ssh配置文件

配置文件路径:

./ssh/config

内容如下所示:

# work
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa


# me
Host github.com.me  # 前缀名可以任意设置
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_me

Host名字随意,接下来会用到。

规则:

从上至下读取config的内容,在每个Host下寻找对应的私钥。

这里将GitHub SSH仓库地址中的git@github.com替换成新建的Host别名.

如:two.github.com那么原地址是:git@github.com:username/reponame.git

替换后应该是:git@two.github.com:username/reponame.git

测试配置

$ ssh -T git@github.com 
Hi ashimidashajia! You've successfully authenticated, but GitHub does not provide shell access.

$ ssh -T git@github.com.me
Hi genglei-cuan! You've successfully authenticated, but GitHub does not provide shell access.

其他注意事项

因为有操作多个github账号,最好取消git的全局设置。

git config --global --unset user.name
git config --global --unset user.email

相关文章

网友评论

      本文标题:一台主机中使用多个github账号

      本文链接:https://www.haomeiwen.com/subject/bhirettx.html