用ssh-keygen生成公匙和私钥
d:\c\learnc>ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/c/Users/Mize/.ssh/id_rsa): Created directory '/c/Users/Mize/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /c/Users/Mize/.ssh/id_rsa. Your public key has been saved in /c/Users/Mize/.ssh/id_rsa.pub. The key fingerprint is: d7:67:5a:c8:6a:ba:3a:62:0e:b3:2c:b9:af:62:4c:cf Mize@MIZE-PC
id_rsa.pub是公匙,id_rsa是私匙,粘贴公匙给github
https://github.com/settings/keys
测试ssh连接,核对github的公匙指纹
github的指纹为:https://help.github.com/articles/what-are-github-s-ssh-key-fingerprints/
d:\c\learnc>ssh git@github.com The authenticity of host 'github.com (192.30.252.121)' can't be established. RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'github.com,192.30.252.121' (RSA) to the list of know n hosts. PTY allocation request failed on channel 0 Hi mizegit! You've successfully authenticated, but GitHub does not provide shell access. Connection to github.com closed.
如果没添加过远程仓库就添加
git remote add origin https://github.com/user/repo.git
参考:https://help.github.com/articles/adding-a-remote/
如果之前为https方式的仓库就,替换https为ssh,ssh方式地址在github仓库网页上可以复制。
d:\c\learnc>git remote -v origin https://github.com/mizegit/learnc.git (fetch) origin https://github.com/mizegit/learnc.git (push) d:\c\learnc>git remote set-url origin git@github.com:mizegit/learnc.git d:\c\learnc>git remote -v origin git@github.com:mizegit/learnc.git (fetch) origin git@github.com:mizegit/learnc.git (push)
下次git push origin master就不会提示输入账号密码了,
https也可以通过修改配置避免输入账号密码:
git remote set-url origin https://[userName]:[password]@github.com/[username]/project.git