我最近切换到将我的存储库同步到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

其他回答

只需将登录凭据作为URL的一部分:

git remote rm origin
git remote add origin https://username:mypassword@github.com/path/to/repo.git

注意:我不建议使用这种方法,但如果你很匆忙,而其他方法都不奏效,你可以使用这种方法。

这对我有用我使用的是Windows 10

git config --global credential.helper wincred

你可以使用

git config credential.helper store

当您下次使用pull或push输入密码时,它将以明文形式存储在文件.git凭据中(有点不安全,但只需将其放入受保护的文件夹中)。

如本页所述:

git凭证存储

为了安全起见,最好使用凭据,但您可以使用缓存将其保存一段时间:

git config --global credential.helper cache
git config credential.helper 'cache --timeout=3600'

您的凭据将保存3600秒。

我并没有立即意识到我需要先下载助手!我在Atlassian的永久认证Git存储库中找到了credential.helper下载。

报价:

如果您想在OS X上使用带有凭据缓存的Git,请执行以下步骤:

下载二进制git凭证osxkeychain。

运行以下命令以确保二进制文件可执行:

chmod a+x git-credential-osxkeychain

将其放在/usr/local/bin目录中。

运行以下命令:

git config --global credential.helper osxkeychain