github一把公钥只能用于一个github帐户,若是想在同一主机上给两个属于不一样帐户的仓库提交时,必须在本地建立两对公/私钥匙,分别把两把公钥给两个账号。git
或者有时候,你公司内部使用的gitlab,同时你我的又有github,你想用同一个公钥将仓库分别提交到github和gitlab。github
生成第一把公钥: ssh-keygen -t rsa -C "kobe@email.com" # 设置名称为id_rsa_kobe Enter file in which to save the key ((/home/xxxx/.ssh/id_rsa)): id_rsa_kobe #添加到SSH agent中 ssh-add id_rsa_kobe 制造第二把公钥: ssh-keygen -t rsa -C "jordan@email.com" # 设置名称为id_rsa_jordan Enter file in which to save the key ((/home/xxxx/.ssh/id_rsa)): id_rsa_jordan #添加到SSH agent中 ssh-add id_rsa_jordan
# 在.ssh目录下配置config文件: Host kobe HostName github.com User git IdentityFile ~/.ssh/id_rsa_kobe Host jordan HostName github.com User git IdentityFile ~/.ssh/id_rsa_jordan
ssh -T kobe Hi kobe! You've successfully authenticated, but GitHub does not provide shel l access. ssh -T ranpop Hi jordan! You've successfully authenticated, but GitHub does not provide shel l access.
对于kobe账号下的仓库: git clone kobe:githubname/repository.git (原地址是:git@github.com:githubname/repository.git,替换后应该是:kobe:githubname/repository.git) 对于ranpop账号下的仓库: git clone jordan::githubname/repository.git (原地址是:git@github.com:githubname/repository.git,替换后应该是:jordan:githubname/repository.git)
# 若是已经使用原地址克隆过了,能够使用以下命令修改 git remote set-url origin kobe:githubname/repository.git # 若是是本地新建的仓库,能够使用以下命令添加 git remote add origin jordan:githubname/repository.git
# 在.ssh目录下配置config文件: Host github HostName github.com User git IdentityFile ~/.ssh/id_rsa_kobe Host gitlab HostName gitlab.com User git IdentityFile ~/.ssh/id_rsa_kobe
ssh -T github Hi kobe! You've successfully authenticated, but GitHub does not provide shel l access. ssh -T gitlab Welcome to GitLab, @kobe!
git remote add github github:githubname/repository.git git remote add gitlab gitlab:githubname/repository.git
# 推送master分支到github git push github master # 推送master分支到gitlab git push gitlab master