1、什么是SSH?

Secure Shell是一种加密的网络传输协议,简称SSH。有开源实现OpenSSH和商业实现。

想要了解SSH工作原理,可以看看这篇文章

2、为什么会出现Key is already in use这个错?

因为公钥只能添加到一个远程仓库

3、我为什么要配置多个ssh密钥对?

因为我想把我的本地仓库推送到不同到远程仓库,比如我有两个Github账号,想把本地同一份代码推到两个不同到账号到对应仓库里。

这个时候,同一个公钥在绑定一个账号后,再绑定另一个账号,就会报出错误Key is already in use

4、如何配置多个ssh密钥对?

4.1、为每个远程仓库生成一份ssh密钥对

  1. 生成本地ssh密钥对

ssh-keygen -t rsa -C 'your_email@example.com' -f ~/.ssh/id_rsa_github

需要把your_email@example.com修改成自己到邮箱;
id_rsa_github是保存到文件名,可以任意取,但是最好有含义.

  1. 第一步执行后,会生成id_rsa_githubid_rsa_github.pub两个文件

  2. 编辑config文件,映射不同到远程仓库账户

~/.ssh/目录下,编辑config文件,如果不存在新建

内容如下:

# 个人的GitHub公钥
Host github.com  # host名是自己取,任意,但是最好有意义
HostName github.com  # 域名必须是远程仓库的域名,不能随便写,其他如gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github_xinrong2019  # key的路径
#指定特定的ssh私钥文件

# 公司的's gitee.com
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# 指定特定的ssh私钥文件

# 另一个github账号
Host github_2.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/jameingh

上面的配置为三个远程仓库账号配置了映射。

  1. 将不同账户的公钥配置到对应的Git服务提供商的配置中保存。

比如点击这里在GitHub上添加SSH密钥

  1. 测试是否添加成功

测试Hostgithub.com的密钥对是否添加成功

ssh -T git@github.com

Hi xinrong2019! You've successfully authenticated, but GitHub does not provide shell access.

测试Hostgithub_2.com的密钥对是否添加成功

ssh -T git@github_2.com

Hi jameingh! You've successfully authenticated, but GitHub does not provide shell access.

4.2、将本地仓库绑定到多个远程仓库,这样就可以每次选择提交到不同的仓库

  1. 先删除已关联的名为origin的远程库:

git remote rm origin

  1. 分别关联GitHub和Gitee仓库

git remote add github git@github.com:SixGodFlowerDewWater1029/mindmap.git

git remote add gitee git@gitee.com:JavaLoveGo/mindmap.git

  1. 配置本地仓库绑定多个远程仓库(可选)

进入.git目录,修改config文件,执行了上面关联仓库的命令后,一般来说会自动加入到config文件中。没有的话,自己手动添加。

文件内容格式

[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[remote "github"]
	url = git@github_2.com:SixGodFlowerDewWater1029/mindmap.git
	fetch = +refs/heads/*:refs/remotes/github/*
[remote "gitee"]
	url = git@gitee.com:JavaLoveGo/mindmap.git
	fetch = +refs/heads/*:refs/remotes/gitee/*
  1. 查看本地映射的仓库

回到项目根目录,执行git remote -v,查看映射的远程仓库

gitee	git@gitee.com:JavaLoveGo/mindmap.git (fetch)
gitee	git@gitee.com:JavaLoveGo/mindmap.git (push)
github	git@github_2.com:SixGodFlowerDewWater1029/mindmap.git (fetch)
github	git@github_2.com:SixGodFlowerDewWater1029/mindmap.git (push)
  1. 分别在两个远程仓库上拉代码

git pull github master

git pull gitee master

  1. 推送代码

git push github master

git push gitee master

  1. 可能的错误1:推送代码失败
Push failed
ERROR: Permission to SixGodFlowerDewWater1029/mindmap.git denied to xinrong2019.
Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.

这是因为远程仓库关联的不对。

执行git remote -v,看到的是如下的映射

github	git@github.com:SixGodFlowerDewWater1029/mindmap.git (fetch)
github	git@github.com:SixGodFlowerDewWater1029/mindmap.git (push)

这里的github.com.ssh目录下config文件中配置的Host,确定github.com和账号xinrong2019是不是关联正确。

就是说,你是不是把公钥配置在了xinrong2019账号下。

我这里,github.com是配置在了jameingh账号下,所以上面报错说xinrong2019没有权限,拒绝访问。

jameingh对应的Hostgithub_2.com,修改项目根目录下.git/config文件中的配置

  1. 可能的错误2: git pull失败
Can't Update
No tracked branch configured for branch master or the branch doesn't exist.
To make your branch track a remote branch call, for example,
git branch --set-upstream-to=origin/master master (show balloon)

设置本地仓库对应的远程仓库映射,从哪个远程仓库等对应分支拉代码。
git branch --set-upstream-to=gitee/master master

5、参考

1、配置多个ssh公钥,解决Key is already in use

2、多个sshkey对应多个不同的github账号

3、git同步代码至github和gitee(码云)

Time waits for no one.