git每次pull提示需要密碼
錯誤提示
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命令生成
ssh-keygen -t rsa -C "Your Email Address" -f "~/.ssh/gitlab_id_rsa"
2. 私鑰驗證錯誤
git拉取代碼時沒有配置私鑰認證
使用以下命令添加認證到ssh
ssh-add ~/.ssh/gitlab_id_rsa
使用以上命令,只能對當前環境生效,重啟或新開終端會繼續提示輸入密碼
永久解決:在~/.ssh/config
文件中(沒有則新建),指定對應git服務使用的認證秘鑰文件即可
# GitLab.com
Host github.com
Preferredauthentications publickey
IdentityFile ~/.ssh/github_id_rsa
# Private GitLab instance
Host gitlab.xxx.com
Preferredauthentications publickey
IdentityFile ~/.ssh/gitlab_id_rsa
結束
- 直接提示輸入密碼的,為秘鑰認證失敗,使用以上方法即可解決
- 提示輸入賬號和密碼的,為賬號認證失敗,使用
git config --global credential.helper store
命令,可輸入一次密碼后永久存儲