有時候我們需要在不同的Repository使用不同的private key,尤其是工作和私人的Repository
目標
我們希望切換到不同的Local Repository下git pull或git push這些和remote repository的指令它就自動使用指定的private key
第一步:到~/.ssh/config新增config
如果有兩個不同的Repository使用不同的key,就需要新增兩個設定
這裡用your-repo-a和your-repo-b來表示
再把Host複製起來,下一步會用到
Host your-repo-a HostName github.com User git IdentityFile /home/tony/.ssh/your_repo_a_id_rsa Host your-repo-b HostName github.com User git IdentityFile /home/tony/.ssh/your_repo_b_id_rsa
第二步:到Local Repository的.git/config
依照上面的設定host,修改remote hostname
[remote "origin"] url = git@your-repo-a:tonyciou/a-project.git
這樣就搞定了!