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

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


当前回答

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

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

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

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

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

其他回答

有一种简单、老式的方法可以将用户凭据存储在HTTPS URL中:

https://user:password@github.com/...

您可以使用git remote set URL<remote repo><URL>更改URL

这种方法的明显缺点是必须以纯文本存储密码。您仍然可以只输入用户名(https://user@github.com/…),这至少可以为您省去一半的麻烦。

您可能更喜欢切换到SSH或使用GitHub客户端软件。

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回购

在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选择设备代码访问链接并输入输出中提供的代码

使用凭据存储。

对于OS X和Linux上的Git 2.11+,请使用Git内置的凭据存储:

git config --global credential.helper libsecret

对于Windows上的msysgit 1.7.9+:

git config --global credential.helper wincred

对于OS X上的Git 1.7.9+,请使用:

git config --global credential.helper osxkeychain

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

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

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

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

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