我最近切换到将我的存储库同步到GitHub上的https://(由于防火墙问题),它每次都会要求输入密码。
有没有办法缓存凭据,而不是每次git推送时都进行身份验证?
我最近切换到将我的存储库同步到GitHub上的https://(由于防火墙问题),它每次都会要求输入密码。
有没有办法缓存凭据,而不是每次git推送时都进行身份验证?
当前回答
我在MacOS上也遇到了这个问题,以下命令对我有效:
rm -rf ~/.git-credentials
这是一种强制方法,可以真正删除所有git凭据。下次我使用push命令时,瞧:系统会提示我输入用户名和密码(或令牌)。
其他回答
你可以使用
git config credential.helper store
当您下次使用pull或push输入密码时,它将以明文形式存储在文件.git凭据中(有点不安全,但只需将其放入受保护的文件夹中)。
如本页所述:
git凭证存储
自Git 1.7.9(2012年发布)以来,Git中有一种巧妙的机制,可以避免一直为HTTP/HTTPS输入密码,称为凭据助手。
您可以只使用以下凭据助手之一:
git config --global credential.helper cache
credential.helper缓存值告诉Git将密码缓存在内存中一段特定的时间。默认值为15分钟,您可以通过以下方式设置更长的超时:
# Cache for 1 hour
git config --global credential.helper "cache --timeout=3600"
# Cache for 1 day
git config --global credential.helper "cache --timeout=86400"
# Cache for 1 week
git config --global credential.helper "cache --timeout=604800"
如果需要,您还可以永久存储凭据,请参阅下面的其他答案。
GitHub的帮助还建议,如果您使用Mac OS X并使用Homebrew安装Git,您可以使用本机Mac OS X密钥库:
git config --global credential.helper osxkeychain
对于Windows,有一个名为GitCredentialManagerforWindows或wincred的助手。
git config --global credential.helper wincred # obsolete
使用Git for Windows 2.7.3+(2016年3月):
git config --global credential.helper manager
对于Linux,您将使用(在2011年)gnomekeyring(或其他keyring实现,如KWallet)。
如今(2020年),这将是(在Linux上)
Fedora公司
sudo dnf install git-credential-libsecret
git config --global credential.helper /usr/libexec/git-core/git-credential-libsecret
Ubuntu(Ubuntu)
sudo apt-get install libsecret-1-0 libsecret-1-dev
cd /usr/share/doc/git/contrib/credential/libsecret
sudo make
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret
我知道这不是一个安全的解决方案,但有时你只需要一个简单的解决方案-而不需要安装任何其他东西。由于helper=store不适合我,我创建了一个虚拟助手:
创建一个脚本并将其放在用户bin文件夹中,此处名为credfake,该脚本将提供您的用户名和密码:
#!/bin/bash
while read line
do
echo "$line"
done < "/dev/stdin"
echo username=mahuser
echo password=MahSecret12345
使其可执行:
chmod u+x /home/mahuser/bin/credfake
然后在git中配置它:
git config --global credential.helper /home/mahuser/bin/credfake
(或不使用--仅限一个回购协议的全局)
和-voilá-git将使用此用户+密码。
如果你像我一样使用双因素身份验证,情况就有点不同了。因为我在其他地方找不到好答案,所以我会在这里留下一个,这样也许我可以稍后找到它。
如果您使用的是双因素身份验证,那么指定用户名/密码甚至不起作用——您的访问被拒绝。但您可以使用应用程序访问令牌,并使用Git的凭据助手为您缓存该令牌。以下是相关链接:
设置命令行以使用2-factor auth(搜索标题为“它如何为命令行Git工作?”的部分)凭据缓存
我不记得我是在哪里看到的,但当你被要求输入用户名时,你就会在那里粘贴应用程序访问令牌。然后将密码留空。它在我的Mac上运行。
如果您正在使用osxkeychain,并且有一个令牌过期,并希望更新它,请执行以下步骤:
在终端中运行,然后按enter键两次。
git credential-osxkeychain erase
host=github.com
protocol=https
现在应该会提示您输入用户名/密码。然而,有时这似乎并不“需要”,你必须继续重新进入。
如果是,请重新启动计算机。现在,下次运行git命令并输入用户名/密码时,它将被保存。