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

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


当前回答

克隆存储库repo后,可以编辑repo/.git/config并添加如下配置:

[user]
    name = you_name
    password = you_password
[credential]
    helper = store

这样就不会再次要求您输入用户名和密码。

其他回答

自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

截至2021,HTTPS远程有一个安全、用户友好的跨平台解决方案。不再输入密码!不再有SSH密钥!不再有个人访问令牌!

安装GitHub开发的Git凭据管理器(下载)。它支持对GitHub、BitBucket、Azure和GitLab的无密码OAuth认证。这意味着您可以在GitHub和其他平台上启用双因素身份验证,大大提高了帐户的安全性。

推送时,您可以选择身份验证方法:

> git push
Select an authentication method for 'https://github.com/':
  1. Web browser (default)
  2. Device code
  3. Personal access token
option (enter for default): 1
info: please complete authentication in your browser...

在Linux上,需要一点点设置。以下内容将凭据缓存在内存中20小时,因此您每天最多只能进行一次身份验证。

git-credential-manager-core configure
git config --global credential.credentialStore cache
git config --global credential.cacheoptions=--timeout 72000

熟悉gnomekeyring或KWallet的高级用户可能更喜欢将凭证存储更改为libsecret。

外观配置:由于我总是在上面的提示下选择“web浏览器”,所以我设置了gitHubAuthModes首选项来跳过选择。GCM的最新版本包括一个GUI,它为授权流添加了额外的点击,我禁用了它。

git config --global credential.gitHubAuthModes browser
git config --global credential.guiPrompt false

您可以使用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。

您还可以编辑bashrc文件并在其中添加脚本。

这将在您启动Git时询问您的密码一次,然后在您注销之前记住密码。

SSH_ENV=$HOME/.ssh/environment
  
# Start the ssh-agent
function start_agent {
    echo "Initializing new SSH agent..."

    # Spawn ssh-agent
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
    echo succeeded
    chmod 600 "${SSH_ENV}"
    . "${SSH_ENV}" > /dev/null
    /usr/bin/ssh-add
}
  
if [ -f "${SSH_ENV}" ]; then
     . "${SSH_ENV}" > /dev/null
   ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
      start_agent;
  }
else
    start_agent;
fi

我在MacOS上也遇到了这个问题,以下命令对我有效:

rm -rf  ~/.git-credentials 

这是一种强制方法,可以真正删除所有git凭据。下次我使用push命令时,瞧:系统会提示我输入用户名和密码(或令牌)。