背景
在不同平台使用git时,我们需要进行不同的配置使得我们能够拉取到不同平台的代码
例如你本地配置了可以拉取github的公钥配置,但这时你公司用了aliyun或者你自己项目是在giteee中,这时我们需要在自己计算机上进行配置
创建ssh-key
我们在之前已经有了一个ssh
接下来我们在命令窗口中再创建一个ssh key
ssh-keygen -t rsa -C "<您的邮箱>"
Generating public/private rsa key pair.
Enter file in which to save the key (~/.ssh/id_rsa):<为了区分多个key,请填写自定义的名称>
为了区分多个 SSH key,以上第3行请不要直接回车,请填写自定义的 key 路径名称以方便后续使用,如 ~/.ssh/codeup_1,~/.ssh/codeup_2,然后点击回车确认。
输入名称后一路回车
命令效果如下
接着查看.ssh下会生成如下文件夹
接着编辑~/.ssh/config文件,有多账号可以如下配置
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_github
IdentitiesOnly yes
# ali云下账号A
Host codeup_1
HostName codeup.aliyun.com
IdentityFile ~/.ssh/codeup_1
PreferredAuthentications publickey
IdentityAgent none
IdentitiesOnly yes
# ali云下账号B
Host codeup_2
HostName codeup.aliyun.com
IdentityFile ~/.ssh/codeup_2
PreferredAuthentications publickey
IdentityAgent none
IdentitiesOnly yes
HostName
:填写托管平台服务地址;Host
:填写别名,方便命令行使用;IdentityFile
:填写证书位置,即刚刚我们在命令中起的名字(codeup_1)PreferredAuthentications
指定 SSH 客户端在尝试身份验证时应优先考虑的认证方法列表。例如,你可以设置为只使用公钥认证,或者优先使用公钥认证,然后再尝试其他 (密码) 认证方式。IdentitiesOnly
yes 指示 SSH 只使用 IdentityFile 指定的身份进行认证,这有助于避免 SSH 代理或默认私钥的干扰
关于hostName就是你随便在你的平台找个仓库,点击ssh克隆时git@后面到.com的位置
仓库中添加公钥
在你的仓库中加入你的公钥,不懂配置公钥的可以看下这个链接中的第二步
接着使用如下命令查看
ssh -t git@codeup_1
正常返回如下图
分割线
补充: 如下添加github多个账户情况
# github2
Host github2
HostName github.com
IdentityFile ~/.ssh/github_id_rsa2
PreferredAuthentications publickey
IdentitiesOnly yes
# github1
Host github1
HostName github.com
IdentityFile ~/.ssh/github_id_rsa
PreferredAuthentications publickey
IdentitiesOnly yes
推送远程仓库需如下操作
git remote add origin git@github2:<your git ssh address>
git push origin master