我最近切换到将我的存储库同步到GitHub上的https://(由于防火墙问题),它每次都会要求输入密码。
有没有办法缓存凭据,而不是每次git推送时都进行身份验证?
我最近切换到将我的存储库同步到GitHub上的https://(由于防火墙问题),它每次都会要求输入密码。
有没有办法缓存凭据,而不是每次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
其他回答
自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
有一种简单、老式的方法可以将用户凭据存储在HTTPS URL中:
https://user:password@github.com/...
您可以使用git remote set URL<remote repo><URL>更改URL
这种方法的明显缺点是必须以纯文本存储密码。您仍然可以只输入用户名(https://user@github.com/…),这至少可以为您省去一半的麻烦。
您可能更喜欢切换到SSH或使用GitHub客户端软件。
双因素身份验证改变了用户对网站的身份验证方式,但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
您可以使用Git凭据管理器(GCM)插件。它目前由GitHub维护。好的是,它将密码保存在Windows凭据存储中,而不是纯文本。
项目的发布页面上有一个安装程序。这还将安装内置凭据管理器的Windows Git官方版本。它允许对GitHub(和其他服务器)进行双重身份验证。并具有用于初始登录的图形界面。
对于Cygwin用户(或已经使用官方Git For Windows的用户),您可能更喜欢手动安装。从发布页面下载zip包。解压缩包,然后运行install.cmd文件。这将安装到您的~/bin文件夹中。(确保~/bin目录位于PATH中。)然后使用以下命令对其进行配置:
git config --global credential.helper manager
然后,Git将在向任何服务器进行身份验证时运行Git-credential-manager.exe。
OAuth(身份验证)
您可以创建自己的个人API令牌(OAuth),并以与使用普通凭据相同的方式使用它(位于:/settings/tokens)。例如:
git remote add fork https://4UTHT0KEN@github.com/foo/bar
git push fork
.netrc文件
另一种方法是在~/.netrc(在Windows上为_netrc)中配置用户/密码,例如。
machine github.com
login USERNAME
password PASSWORD
对于HTTPS,添加额外的行:
protocol https
凭据助手
要在使用HTTPS时在Git中缓存GitHub密码,您可以使用凭据助手告诉Git每次与GitHub对话时记住您的GitHub用户名和密码。
Mac:git config--全局凭证.helper osxkeychain(需要osxkeychain助手),Windows:git-config--全局凭据。helper wincredLinux和其他:git-config--全局凭证.helper缓存
相关:
如何在终端密钥链中存储Linux上的GitHub https密码?如何确保Git不会向我索要GitHub用户名和密码?配置Git客户端,如GitHub for Windows,以不要求身份验证将本地回购推送到具有双重身份验证的GitHub回购