实现原理
在电脑上生成各自配套的SSH Key,这样在Push代码时,可能自动识别是哪个账号。
##生成SSH Key
进入~/.ssh/目录:
cd .ssh
生成SSH Key:
ssh-keygen -t rsa -C "youremail@xxx.com"
回车后,需要设置SSH Key名字,比如:id_rsa_personal,id_rsa_compan等。
##在GitHub上添加Key
用记事本打开ssh_key_name.pub,复制里面的内容,登录https://github.com/settings/keys后添加。
##在本地SSH Agent中添加Key
添加Key:
ssh-add -K ~/.ssh/id_rsa_personal`
`ssh-add -K ~/.ssh/id_rsa_company
查询已添加Key:
ssh-add -l
##配置config文件
在~/.ssh/中创建:
touch config
打开config文件:
open ~/.ssh
配置config文件:
# personal
Host personal.github #这个名字可任意设置
HostName github.com #不修改
User git #不修改
IdentityFile ~/.ssh/id_rsa_personal #对应的Key
# company
Host company.github
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_company
测试是否配置成功:
ssh -T git@personal.github
ssh -T git@company.github
注意@后面是你上面自定义的Host。
##如何使用
当你Clone项目时,需要改成:
git clone git@personal.github:xxx/xxx.git
重命名文件夹:
mv file_name new_flie_name
本地提交:
git add .
git commit -m "xxx"
推送:
git push origin master
附:对于已经克隆的本地项目,在项目中修改配置:
git remote rm origin
git remote add origin git@personal.github:xxx/xxx.git
可以执行 git remote -v 查看是否修改成功。
然后在每个项目中,执行:
git config user.email "xxx@xxx.com"
git config user.name "xxname"
查看是否修改成功:
git config user.name
git config user.email