1、获取额外的 SSH key

使用命令 ssh-keygen -t rsa -C "邮箱地址" 然后一路回车就可以方便得到一个 SSH key。

但如果需要生成额外的 SSH key,在执行上述命令后,还应当修改 SSH key 的默认名称,如:

Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): /c/Users/Administrator/.ssh/id_rsa_user2

现在,就创建好了 id_rsa.pubid_rsa_user2.pub 两个 SSH key。

2、将新密钥添加到 SSH agent 中

因为默认只读取 id_rsa,为了识别新的私钥,需将其添加到 SSH agent 中:

ssh-agent bash
ssh-add ~/.ssh/id_rsa_user2

3、修改 config 文件

.ssh/ 路径下新建一个 config 文件:

Host blog.github.com
	HostName github.com
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/id_rsa
Host www.github.com
	HostName github.com
	PreferredAuthentications publickey
	IdentityFile ~/.ssh/id_rsa_user2

该配置文件指示了 Git 与 GitHub 账户同步时采用的私钥。

4、将 .pub 文件的内容添加到 GitHub 账户中

分别将 id_rsa.pubid_rsa_user2.pub 添加到 GitHub 账户 user-a 与 user-b 中。

输入下述命令测试是否成功:

ssh -T git@blog.github.com
ssh -T git@www.github.com 

如果成功就将 GitHub 端的项目 clone 至本地:

git clone git@blog.github.com:user-a/user-a.github.io.git
git clone git@www.github.com:user-b/user-b.github.io.git

5、在不同的 GitHub 项目上应用 Git 命令

使用 git 命令前,应首先将当前路径设为项目根目录 username.github.io/,即:

cd username.github.io

如果出现如下错误:

fatal: unable to access ‘***’: The requested URL returned error: 403

或者如下错误:

ERROR: Permission to ** denied to **.
fatal: Could not read from remote repository.

则可能需要修改 user-b.github.io/.git/config 文件中的 [remote "origin"] 字段:

[remote "origin"]
	url = git@www.github.com:user-b/user-b.github.io.git

这是因为 git@github.com 将默认使用 .ssh/config 中最先指定的 key,因此需要特别指明。

如果您喜欢这篇文章,欢迎转载、演绎或用于商业目的,但请务必保留作者署名以及本文链接!
Copyright © Pandaman