因为我在公司的Git源码服务器有一套帐号和密码,我我的的Github又有一套帐号和密码,这两个帐号都须要PUSH和PULL代码,若是对Git使用的不太熟练,常常会遇到git@github.com: Permission denied (publickey)
这种错误,那么咱们该如何同时管理多个Git帐号呢?git
这里咱们以MacOS系统为例。github
这里所说的帐号相同,指的是:邮箱相同。
好比我在公司Gitlab用的帐号和Github的帐号就是同一个邮箱,这种状况比较好处理,Gitlab和Github在校验的时候是只认邮箱的。
只要咱们把秘钥,也就是 id_ras.pub
里面的内容在Gitlab和Github上都配置好就能够了。也就说多个Git源码服务器且都是使用同一个邮箱的状况下,咱们只要把id_ras.pub
配置到多个源码服务器就OK了!bash
这个时候就须要咱们指明哪一个帐号使用哪个.pub
文件了。咱们须要在~/.ssh
文件夹下建立名为config
的文件,若是已经存在了该文件则须要修改一下。 config
的内容参考如下写法:服务器
# 第一个帐号为 # Email:fulade1@gmail.com # User:fulade1 host gitlab.ttal.com hostname gitlab.ttal.com Port 65095 User fulade1 IdentityFile /home/Fulade/.ssh/id_rsa # 第二个帐号为 # Email:fulade2@gmail.com # User:fulade2 host gitlab-test.ttal.com hostname gitlab.ttal.com Port 65095 User fulade2 IdentityFile /home/Fulade/.ssh/id_rsa_second # 第三个帐号 # Email:fulade2@gmail.com # User:fulade2 host github.com hostname github.com Port 22 User fulade2 IdentityFile /home/Fulade/.ssh/id_rsa_second # 其中第二个帐号跟第三个帐号相同,只是源码服务器不一样。
由于配置了多个邮箱帐号,全部git的global配置就要删除了。ssh
# 取消全局配置 git config --global --unset user.name git config --global --unset user.email
而后在每一个Repo下配置帐号就能够了ide
# 每一个项目Repo设置本身的user.email git config user.email "xxxx@xx.com" git config user.name "fulade"
这样就能够了。gitlab
指定.pub
文件路径的方法:
须要使用命令ssh-keygen -t rsa -C "fulade@gmail.com"
,接下来输入路径就能够了code
ssh-keygen -t rsa -C "fulade@gmail.com" Generating public/private rsa key pair. Enter file in which to save the key (/Users/Fulade/.ssh/id_rsa):/Users/Fulade/.ssh/id_rsa_second
其中/Users/Fulade/.ssh/id_rsa_second
是要输入的部分。
Have Fun !源码