使用git push origin master是出现如下问题;
Username for 'https://github.com':
但是我由于在公司已经用了公司的gitlab账号已经添加了个rsa,因为没办法再次用我的游戏登录github
解决办法:
git remote set-url origin git+ssh://git@github.com/username/reponame.git
博主在github上下载tiny face的的源代码的时候,遇到git clone命令为:git clone --recursive git@github.com:peiyunh/tiny.git
而当我在ternimal下执行这条语句的时候,出现错误:
Permissiondenied (publickey).
fatal:Could not read from remote repository.
Pleasemake sure you have the correct access rights
and the repository exists.
但是其实执行命令:git clone git@github.com:peiyunh/tiny.git 是没有问题的(不加--recursive参数),于是百度了一番,我理解的是原因是由于你在本地(或者服务器上)没有生成ssh key,你可以在ternimal下执行:
cd ~/.ssh ls来查看是否有文件id_rsa以及文件id_rsa.pub,如下图所示:(我的已经生成了,所以我ls后会显示。)
下面记录下解决办法:
1.首先,如果你没有ssh key的话,在ternimal下输入命令:ssh-keygen -t rsa -C "youremail@example.com", youremail@example.com改为自己的邮箱即可,途中会让你输入密码啥的,不需要管,一路回车即可,会生成你的ssh key。(如果重新生成的话会覆盖之前的ssh key。)
2.然后再ternimal下执行命令:
ssh -v git@github.com
最后两句会出现:
No more authentication methods to try.
Permission denied (publickey).
3.这时候再在ternimal下输入:
ssh-agent -s
然后会提示类似的信息:
SSH_AUTH_SOCK=/tmp/ssh-GTpABX1a05qH/agent.404; export SSH_AUTH_SOCK;
SSH_AGENT_PID=13144; export SSH_AGENT_PID;
echo Agent pid 13144;
4.接着再输入:
ssh-add ~/.ssh/id_rsa
这时候应该会提示:
Identity added: ...(这里是一些ssh key文件路径的信息)
(注意)如果出现错误提示:
Could not open a connection to your authentication agent.
请执行命令:eval `ssh-agent -s`后继续执行命令 ssh-add ~/.ssh/id_rsa,这时候一般没问题啦。
5.打开你刚刚生成的id_rsa.pub,将里面的内容复制,进入你的github账号,在settings下,SSH and GPG keys下new SSH key,title随便取一个名字,然后将id_rsa.pub里的内容复制到Key中,完成后Add SSH Key。
6.最后一步,验证Key
在ternimal下输入命令:
ssh -T git@github.com
提示:Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.
这时候你的问题就解决啦,可以使用命令 git clone --recursive git@github.com:peiyunh/tiny.git 去下载你的代码啦。
git config --global user.name 你的目标用户名;
git config --global user.email 你的目标邮箱名;
ssh-add ~/.ssh/id_rsa
有时候我们明明在github上添加了ssh key,但是在push代码的时候还会要求我们输入用户名密码,这是为什么了?
其中一种可能的原因是,你在克隆代码的时候,使用了https协议,而不是git协议。https协议要求你每次都需要输入用户名以及密码,自由git协议才可以使用ssh-key文件。
查看自己使用的源地址是https还是git
使用git remote -v命令
在这里插入图片描述
看到https开头的就是使用的https协议,找到项目对于的git源,
使用git remote set-url命令来修改源
在这里插入图片描述
在使用git remote -vj就可以查看源是否更改过来了,让后你就可以愉快的git push 而不需要输入用户名密码了。当然,这一切的前提都是你已经添加了ssh-ky.