首先,创建SSH公钥(这里用的是mac系统)
添加创建代码,先添加github账号
$ssh-keygen -t rsa -C "GitEmail@example.com"
# Creates a new ssh key, using the provided email as a label
# Generating public/private rsa key pair.
Enterfileinwhich to save the key (/Users/you/.ssh/id_rsa):[Pressenter]
这里先别按回车,因为我们需要不同账号登陆,我们输入
/Users/you/.ssh/id_rsa_git
回车,记住前面的you每个不同电脑账号显示不一样。改成跟你提示一样的。
创建coding账号的,假设我们名字为id_rsa_coding
$ssh-keygen -t rsa -C "CodingEmail@example.com"
# Creates a new ssh key, using the provided email as a label
# Generating public/private rsa key pair.
Enterfileinwhich to save the key (/Users/you/.ssh/id_rsa):[Pressenter] /Users/you/.ssh/id_rsa_coding //回车
创建配置文件,用来区分不同账号
$vi ~/.ssh/config
用vi编辑器来创建config文件,按键盘i,插入文本。复制修改如下
#one(GitEmail@example.com)
Host git
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_git
User yourName
#two(CodingEmail@example.com)
Host coding
HostName git.coding.net
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_coding
User yourName
然后按键盘的esc,输入 :wq 回车 退出保存

然后再通过命令,把公钥添加到系统
$ssh-add id_rsa_coding id_rsa_git
如果在第一步有输入秘钥的话,这里会让你输入

添加完以后,可以通过如下命令来查看
$ssh-add -l

然后我们把2份pub的内容放到git跟coding里面
不懂请看文档:coding创建公钥

放好以后,我们开始在命令行测试看有没有连接成功~
ssh -T git@git
coding测试
ssh -T git@coding
这个其实等同于,还记得我们的config配置文件么
ssh -T git@github.com
如下图,恭喜你成功了。

然后以后我们克隆项目命令
git clone git@git.coding.net:huangjianyu/TestCoding.git
改为
git clone git@coding:huangjianyu/TestCoding.git
就是把host对应的,clone后就可以直接cd到目录下,操作了。不需要指定账号。


网友评论