我最近切换到将我的存储库同步到GitHub上的https://(由于防火墙问题),它每次都会要求输入密码。
有没有办法缓存凭据,而不是每次git推送时都进行身份验证?
我最近切换到将我的存储库同步到GitHub上的https://(由于防火墙问题),它每次都会要求输入密码。
有没有办法缓存凭据,而不是每次git推送时都进行身份验证?
当前回答
截至2021,HTTPS远程有一个安全、用户友好的跨平台解决方案。不再输入密码!不再有SSH密钥!不再有个人访问令牌!
安装GitHub开发的Git凭据管理器(下载)。它支持对GitHub、BitBucket、Azure和GitLab的无密码OAuth认证。这意味着您可以在GitHub和其他平台上启用双因素身份验证,大大提高了帐户的安全性。
推送时,您可以选择身份验证方法:
> git push
Select an authentication method for 'https://github.com/':
1. Web browser (default)
2. Device code
3. Personal access token
option (enter for default): 1
info: please complete authentication in your browser...
在Linux上,需要一点点设置。以下内容将凭据缓存在内存中20小时,因此您每天最多只能进行一次身份验证。
git-credential-manager-core configure
git config --global credential.credentialStore cache
git config --global credential.cacheoptions=--timeout 72000
熟悉gnomekeyring或KWallet的高级用户可能更喜欢将凭证存储更改为libsecret。
外观配置:由于我总是在上面的提示下选择“web浏览器”,所以我设置了gitHubAuthModes首选项来跳过选择。GCM的最新版本包括一个GUI,它为授权流添加了额外的点击,我禁用了它。
git config --global credential.gitHubAuthModes browser
git config --global credential.guiPrompt false
其他回答
通常你有一个远程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)
如果您正在使用osxkeychain,并且有一个令牌过期,并希望更新它,请执行以下步骤:
在终端中运行,然后按enter键两次。
git credential-osxkeychain erase
host=github.com
protocol=https
现在应该会提示您输入用户名/密码。然而,有时这似乎并不“需要”,你必须继续重新进入。
如果是,请重新启动计算机。现在,下次运行git命令并输入用户名/密码时,它将被保存。
我在MacOS上也遇到了这个问题,以下命令对我有效:
rm -rf ~/.git-credentials
这是一种强制方法,可以真正删除所有git凭据。下次我使用push命令时,瞧:系统会提示我输入用户名和密码(或令牌)。
我并没有立即意识到我需要先下载助手!我在Atlassian的永久认证Git存储库中找到了credential.helper下载。
报价:
如果您想在OS X上使用带有凭据缓存的Git,请执行以下步骤:
下载二进制git凭证osxkeychain。
运行以下命令以确保二进制文件可执行:
chmod a+x git-credential-osxkeychain
将其放在/usr/local/bin目录中。
运行以下命令:
git config --global credential.helper osxkeychain
双因素身份验证改变了用户对网站的身份验证方式,但Git仍然假设用户可以从内存中键入密码。
介绍git credential oauth:一个git凭据助手,可以使用oauth安全认证GitHub、GitLab、BitBucket和其他伪造文件。
没有更多密码!不再有个人访问令牌!不再有SSH密钥!第一次推送时,助手将打开浏览器窗口进行身份验证。缓存超时内的后续推送不需要交互。
从安装https://github.com/hickford/git-credential-oauth/releases/
配置方式:
git config --global --unset-all credential.helper
git config --global --add credential.helper "cache --timeout 7200" # two hours
git config --global --add credential.helper oauth