版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZWE7616175/article/details/82290617
对于初学git的人来说,在每次使用push和pull时需要输入用户名和密码,原因是我们git clone下载代码时连接的是https://而不是git@git(ssh)的形式 ,每次都要用https的方式访问远程库。当我们操作git pull/push到远程的时候,总是提示我们输入账号和密码才能操作成功,频繁的输入账号和密码会很麻烦。
为什么GitHub需要SSH Key呢?因为GitHub需要识别出你推送的提交确实是你推送的,而不是别人冒充的,而Git支持SSH协议,所以,GitHub只要知道了你的公钥,就可以确认只有你自己才能推送。
下面给一种解决方法:
1.git remote -v(查看使用的传输协议)
原因是使用https方法来push,为了安全起见,改成ssh方式就可以解决了。
2.git remote remove origin
用来移除原来的连接。
3.git remote add origin [email protected]:zwe7616175/test.git
建立新的连接。
4.git remote -v
再次输入命令后,发现连接方式已经改变。
5.设置完成后,push和pull时就不需要输入用户名和密码了。
[zwe@localhost test]$ git remote -v
origin https://github.com/zwe7616175/test.git (fetch)
origin https://github.com/zwe7616175/test.git (push)
[zwe@localhost test]$ git remote remove origin
[zwe@localhost test]$ git remote add origin git@github.com:zwe7616175/test.git
[zwe@localhost test]$ git remote -v
origin git@github.com:zwe7616175/test.git (fetch)
origin git@github.com:zwe7616175/test.git (push)
还有一种解决方案时:
git config -global credential.helper store
作用是https记住密码。
输入这个命令后,会在本地生成一个文本,里面记录账户和密码,设置好后,再一次操作git pull,还是会提醒你输入账号密码,这一次后就不需要输入密码了。