使用场景
当我们在公司办公的环境下,会遇到既想用公司的账号也想用自己的账号时产生的问题
步骤
1.首先现在电脑端安装好git(已经安装的跳过)
2.打开git bash, 生成 ssh key
1.cd C:\Users\用户名\.ssh
2.ssh-keygen -t rsa -C "your_email@youremail.com"
比如我的是:id_rsa_temp 此时会在C:\Users\用户名.ssh 多出
3.生成配置文件
vim config
复制内容如下:
Host temp
Hostname github.com
User zhangsan
IdentityFile ~/.ssh/id_rsa_temp
Host temp1
Hostname github.com
User lisi
IdentityFile ~/.ssh/id_rsa_temp1
上述字段详解
Host: 你起得别名(很重要)
Hostname: git服务器名字 一般都是github.com 或者你们公司自己的gitlab
User: 你的用户名
IdentityFile: 你的ssh key的文件 ~/.ssh/id_rsa_temp 建议不要动 只需替换id_rsa_temp 这个文件名即可,文件名是步骤二中的输入的文件名
4.测试配置
ssh -T git@temp
temp是别名
出现如上: 就是成功了
可能会出现下面报错:
- Permission denied (publickey).
原因: 可能是没有与github上的账号成功建立密钥对。
解决方案:将第二步生成的.pub文件内容复制到如下图
2./c/Users/.../.ssh/config line 4: garbage at end of line; 这种类似的错误
解决方案:删掉非法字符
4.clone项目
cd 项目目录
git init
git config user.name "Your name"
git config user.email your_email@gmail.com
如果设置了全局的,建议取消
git config --global --unset user.name
原本是这样
git clone git@github.com:zhangsan/CompoRepos.git
改为
git clone git@temp:zhangsan/koa.git
就是把 github.com 改为 第三步中配置的别名