我最近切换到将我的存储库同步到GitHub上的https://(由于防火墙问题),它每次都会要求输入密码。
有没有办法缓存凭据,而不是每次git推送时都进行身份验证?
我最近切换到将我的存储库同步到GitHub上的https://(由于防火墙问题),它每次都会要求输入密码。
有没有办法缓存凭据,而不是每次git推送时都进行身份验证?
当前回答
通常你有一个远程URL,像这样,
git remote -v
origin https://gitlab.com/username/Repo.git (fetch)
origin https://gitlab.com/username/Repo.git (push)
如果您想在使用git push时跳过用户名和密码,请尝试以下操作:
git remote set-url origin https://username:password@gitlab.com/username/Repo.git
我刚刚向origin添加了相同的URL(包括密码在内的用户详细信息)。
注意:如果用户名是电子邮件Id,则不起作用。
git remote -v
origin https://username:password@gitlab.com/username/Repo.git (fetch)
origin https://username:password@gitlab.com/username/Repo.git (push)
其他回答
您可以使用凭据助手。
git config --global credential.helper 'cache --timeout=x'
其中x是秒数。
只需将登录凭据作为URL的一部分:
git remote rm origin
git remote add origin https://username:mypassword@github.com/path/to/repo.git
注意:我不建议使用这种方法,但如果你很匆忙,而其他方法都不奏效,你可以使用这种方法。
在Ubuntu上使用GitCredentialManager(GCM)在本地缓存凭据,在Ubuntu 20.04和18.04上进行了测试,但应该可以在其他Linux发行版上运行。
设置git凭据管理器:
curl -LO https://raw.githubusercontent.com/GitCredentialManager/git-credential-manager/main/src/linux/Packaging.Linux/install-from-source.sh
sh ./install-from-source.sh
git-credential-manager-core configure
git config --global credential.credentialStore cache
git config --global credential.cacheoptions "--timeout 72000"
sudo rm -rf git-credential-manager/
sudo rm install-from-source.sh
转到回购并运行git fetch选择设备代码访问链接并输入输出中提供的代码
如果你不想像Mark所说的那样以明文形式存储密码,你可以使用不同的GitHub URL进行抓取,而不是推送。在配置文件中的[远程“原点”]下:
url = git://github.com/you/projectName.git
pushurl = git@github.com:you/projectName.git
当您推送时,它仍然会要求输入密码,但当您获取时,它不会要求输入密码。
为了安全起见,最好使用凭据,但您可以使用缓存将其保存一段时间:
git config --global credential.helper cache
git config credential.helper 'cache --timeout=3600'
您的凭据将保存3600秒。