提示:git每次pull提示需要密码


错误提示

1
2
3
Administrator@DEEP MINGW64 /f/code/xxx-service (master)
$ git pull
git@gitlab.xxx.com's password:

原因

只提示输入密码的情况,一般是秘钥验证失败。二种情况


1、公钥验证错误

解决方法如下,以github为例

https://github.com/settings/keys 界面,将生成的公钥添加到SSH keys项中

如没有则使用ssh-keygen命令生成

1
ssh-keygen -t rsa -C "Your Email Address" -f "~/.ssh/gitlab_id_rsa"

2、私钥验证错误

git拉取代码时没有配置私钥认证

使用以下命令添加认证到ssh

1
ssh-add ~/.ssh/gitlab_id_rsa

使用以上命令,只能对当前环境生效,重启或新开终端会继续提示输入密码

永久解决:在~/.ssh/config文件中(没有则新建),指定对应git服务使用的认证秘钥文件即可

1
2
3
4
5
6
7
8
# GitLab.com 密钥
Host github.com
Preferredauthentications publickey
IdentityFile ~/.ssh/github_id_rsa
# GitLab.com 密钥
Host gitlab.xxx.com
Preferredauthentications publickey
IdentityFile ~/.ssh/gitlab_id_rsa

总结

  1. 直接提示输入密码的,为秘钥认证失败,使用以上方法即可解决
  2. 提示输入账号和密码的,为账号认证失败,使用git config --global credential.helper store命令,可输入一次密码后永久存储