最近在oschina上託管項目,oschina上的項目都是用git來管理。git有個很麻煩的地方就是每次提交代碼,都要求輸入oschina的用戶名和密碼進行驗證,極大的影響效率。oschina提供了SSH Key訪問的方法,該方法只要在oschina上添加公鑰,在本地使用密鑰就可以免密碼連接,參考教程。
首先用ssh-keygen生成sshkey
ssh-keygen -t rsa -C "[email protected]" -f "d:\id_rsa"
[email protected]是個人郵箱
d:\id_rsa 是生成的sshkey文件
接下來會要求輸入私鑰密碼,如果想留空可以直接按回車(Enter)
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
完成後會有如下提示,下面的key值可能不一樣
The key fingerprint is:
bf:3c:17:0b:16:31:86:bb:c4:f3:06:75:7d:83:72:78 [email protected]
最後生成兩個文件id_rsa和id_rsa.pub,把這兩個文件放到.ssh文件夾下,windows中.ssh文件夾一般在系統盤的用戶下(c:\users\)
用記事本把id_rsa.pub打開,把文本添加到oschina的公鑰列表中
在git bash中輸入
ssh -T [email protected]
返回Welcome to Git@OSC, 你的名字! 表示添加成功。
注意事項:
生成的sshkey文件一定要命名爲id_rsa,因爲ssh默認讀id_rsa的sshkey。
在完成以上配置後,提交代碼還是需要輸入用戶名和密碼,可以到本地git repository的.git\config文件,如果url使用的是https協議,改爲git協議即可。
修改前
[remote "origin"]
url = https://git.oschina.net/oschina/git-osc.git
fetch = +refs/heads/*:refs/remotes/origin/*
修改後
[remote "origin"]
url = [email protected]:oschina/git-osc.git
fetch = +refs/heads/*:refs/remotes/origin/*