为不同Git账户生成对应的SSH秘钥(Mac)
1.切换到.ssh目录下
cd ~/.ssh
2.生成ssh key(有对应多少个git账户就生成多少不同的ssh key,邮箱记得换(随便写))
ssh-keygen -t rsa -C "person@longtiequan.com"
3.取一个对应的名字,记住(person_longtiequan),然年两下回车,密码不要设置(不然每次推拉都要你输)
macdeMacBook-Pro:.ssh wangzhe$ ssh-keygen -t rsa -C "person@longtiequan.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/wangzhe/.ssh/id_rsa): person_longtiequan
4.去对应的git远端绑定该生成的ssh key,获取方法:
cat person_longtiequan.pub
5.在.ssh目录下生成一个config
文件
vim config
6.在config
中为各个git账号的Host配置别名和对应的ssh key,如下配置config
内容,完成后:wq
退出保存(之前用shift + zz退出会有问题,原因不详)
#个人
Host longtiequan.com # host别名
HostName code.aliyun.com # 原host
IdentityFile ~/.ssh/person_longtiequan # 对应的ssh key 路径
#工作1
Host workfirst.com
HostName code.aliyun.com
IdentityFile ~/.ssh/work_first
#工作2
Host worksecond.com
HostName code.aliyun.com
IdentityFile ~/.ssh/work_second
7.在对应的项目git config文件中,修改host为设置好的别名host
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = git@longtiequan.com:Jason/BaseProject.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
PS
此方法可完美实现不同git账号在同一台电脑下的,push和pull等操作,但是clone会报错,解决方案是在git命令前,加一段指定ssh key的命令:
GIT_SSH_COMMAND="ssh -i ~/.ssh/person_longtiequan" git clone url