场景
由于在 coding 和 github 上面都托管着项目,在实际应用中不想每次进行代码提交和代码拉取时输入账号密码,但由于git 上面和coding的账号不一致,导致配置完一个,另一个平台的账号就无法使用的情况。
解决方案
- 生成签名
- 修改 ssh_config (mac 的位置:/etc/ssh/ssh_config)
- 添加签名 ssh-add
- 测试配置结果
生成签名
ssh-keygen -t rsa -C "coding"
ssh-keygen -t rsa -C "github"
修改ssh_config
Host github
Hostname github.com
Port 22
User yyy
IdentityFile /Users/xxx/.ssh/github_yyy
Host coding
Hostname coding.net
Port 22
User xxx
IdentityFile /Users/xxx/.ssh/coding_xxx
添加签名
ssh-add /Users/xxx/.ssh/github_yyy
ssh-add /Users/xxx/.ssh/coding_xxx
测试配置结果
ssh -T git@github.com
Hi yyy! You've successfully authenticated, but GitHub does not provide shell access.
ssh -T git@git.coding.net
Hello xxx! You've connected to Coding.net via SSH successfully!
常见问题:
1、修改 http 请求为 ssh
➜ laravel_exam git:(master) git push origin master
remote: Permission to xxx/laravel_exam.git denied to XXX.
[core]
fatal: unable to access 'https://github.com/xxx/laravel_exam.git/': The requested URL returned error: 403
➜ laravel_exam git:(master) ssh -T git@github.com
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.
➜ laravel_exam git:(master) git remote -v
origin https://github.com/xxx/laravel_exam.git (fetch)
origin https://github.com/xxx/laravel_exam.git (push)
➜ laravel_exam git:(master) git remote set-url origin git@github.com:xxx/laravel_exam.git
➜ laravel_exam git:(master) git remote -v
origin git@github.com:xxx/laravel_exam.git (fetch)
origin git@github.com:xxx/laravel_exam.git (push)