我最近切换到将我的存储库同步到GitHub上的https://(由于防火墙问题),它每次都会要求输入密码。

有没有办法缓存凭据,而不是每次git推送时都进行身份验证?


当前回答

双因素身份验证改变了用户对网站的身份验证方式,但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 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

如果你不想像Mark所说的那样以明文形式存储密码,你可以使用不同的GitHub URL进行抓取,而不是推送。在配置文件中的[远程“原点”]下:

url = git://github.com/you/projectName.git
pushurl = git@github.com:you/projectName.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选择设备代码访问链接并输入输出中提供的代码

如果您正在使用osxkeychain,并且有一个令牌过期,并希望更新它,请执行以下步骤:

在终端中运行,然后按enter键两次。

git credential-osxkeychain erase
 host=github.com
 protocol=https

现在应该会提示您输入用户名/密码。然而,有时这似乎并不“需要”,你必须继续重新进入。

如果是,请重新启动计算机。现在,下次运行git命令并输入用户名/密码时,它将被保存。

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

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

您的凭据将保存3600秒。