git push 每次都需要输入用户名和密码

解决方案:

每次都需要输入用户名和密码是因为你采用的是 https 方式提交代码, 如果采用的是 ssh 方式只需要在版本库中添加用户的 sha 的key就可以实现提交时无需输入用户名和密码。

详细步骤:

步骤1:

如果你的版本库已经用https 方式创建好了,那么就需要先删除原来的提交方式。在终端执行以下指令:

 git remote rm origin
 git remote add origin git@github.com:(用户名)/版本库名

这里我提供一下我的具体例子:
https: https://github.com/用户名/GitTest.git
ssh: git@github.com:用户名/GitTest.git
我是怎么知道的呢?如果你在创建版本库时选择不创建README.md,系统会提示你创建:

https:
…or create a new repository on the command line
echo # GitTest >> README.md

git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/Sugerming/GitTest.git
git push -u origin master

…or push an existing repository from the command line
git remote add origin https://github.com/Sugerming/GitTest.git
git push -u origin master



ssh:
…or create a new repository on the command line
echo # GitTest >> README.md

git init
git add README.md
git commit -m "first commit"
git remote add origin git@github.com:Sugerming/GitTest.git
git push -u origin master


…or push an existing repository from the command line
git remote add origin git@github.com:Sugerming/GitTest.git
git push -u origin master

步骤2:

然后这个时候你使用下面指令提交代码:

git push -u origin master

系统会提示:

The authenticity of host 'github.com (192.30.252.131)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.252.131' (RSA) to the list of known h                             osts.
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights

说明你权限不够。所以这时你需要在本地创建自己的RSA的key。如下:

ssh-keygen -t rsa -C "用户名"

然后系统会问你保存路径等东西,我直接enter跳过了。

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/AlexYi/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

然后系统会生成一些东西:

Your identification has been saved in /c/Users/AlexYi/.ssh/id_rsa.
Your public key has been saved in /c/Users/AlexYi/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:rxfK05d7oZWpDvQ5dRQM0Na7...
The key's randomart image is:
+---[RSA 2048]----+
|           .o.+. |
|             o o.|
|            .   o|
|               o |
...

最主要的是告诉你,你的可以在:

Your public key has been saved in /c/Users/AlexYi/.ssh/id_rsa.pub

找到这个文件,然后用记事本打开,就可以看到自己的key:

ssh-rsa AAAAB3NzaC1yc2EAAAADA...

步骤3:

然后将生成的rsa 的key添加到版本库中即可,方法:
打开自己的版本库,点击右边的 Settings 进入配置页。
然后点击左边导航栏的: Deploy keys 进入添加key页面
然后点击: Add deploy keys ,将自己的内容输入进去就可以了。
这样就完成了。

最后继续提交更改的代码,使用:

git push -u origin master

可以提交成功。

补充:

如果要使用 git push简短提交代码:

git push

需要配置 :

git config --global push.default simple

或者:

git config --global push.default matching

区别在于,前者只提交你当前所在的分支,而后者会提交本地所有的分支。具体的自己去查一下就明白了

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值