寫在前面的話
github賬號,工作有一個,自己有一個。但是默認下使用ssh key在git push時只有默認賬號能免輸入賬號和密碼。
如果想讓另一個賬號在代碼push時免賬號和密碼,請看這篇文章http://omiga.org/blog/archives/2269。
如果你建立並配置了.ssh/config文件后,但卻不知道怎么使用。這篇文章是為你准備的。
Host me.github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/me_rsa Host work.github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/work_rsa
多個github帳號的SSH key切換
在切換之前請確保配置是正確的。
$ ssh -T git@github.com Hi stefano! You've successfully authenticated, but GitHub does not provide shell access. $ ssh -T git@work.github.com Hi stefano! You've successfully authenticated, but GitHub does not provide shell access.
-T 參數是什么意思,在下並不知道。如有大牛不幸看到此段,請不吝賜教。
現在就各種情況給出不同的做法:
1. 倉庫已經在本地了(本地創建或者已經clone到本地)
因為是默認用戶登錄,如果push代碼會被拒絕。解決辦法:在當前的倉庫目錄下
vi .git/config #更改[remote "origin"]項中的url中的 #work.github.com 對應上面配置的host url = git@work.github.com:username/repo.git
2. 要clone任意配置的host對應的賬戶下的倉庫
#me.github.com對應一個賬號
git clone git@me.github.com:username/repo.git
http://stackoverflow.com/questions/3225862/multiple-github-accounts-ssh-config
http://omiga.org/blog/archives/2269
https://help.github.com/articles/generating-ssh-keys
http://blog.csdn.net/npjocj/article/details/16826119