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

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


当前回答

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

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

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

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

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

其他回答

应使用身份验证令牌而不是帐户密码。转到GitHub设置/应用程序,然后创建个人访问令牌。令牌的使用方式与密码的使用方式相同。

令牌旨在允许用户不使用帐户密码进行项目工作。仅在执行管理工作时使用密码,如创建新令牌或撤销旧令牌。


与授予用户对GitHub帐户的全部访问权限的令牌或密码不同,可以使用特定于项目的部署密钥来授予对单个项目存储库的访问权限。当您仍然可以使用普通凭据访问其他Git帐户或项目时,可以在以下步骤中将Git项目配置为使用此不同的密钥:

编写一个SSH配置文件,其中包含Host、部署密钥的IdentityFile、UserKnownHostsFile和User(尽管我认为您不需要)。编写一个SSH包装器shell脚本,该脚本实际上是SSH-F/path/to/your/config$*将GIT_SSH=/path/to/your/wapper前置到普通GIT命令前面。这里git remote(原点)必须使用git@github.com:user/project.git格式。

您还可以编辑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

克隆存储库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

composer文档提到,您可以阻止它使用GitHub API,这样它的行为就像gitclone:

如果在GitHub存储库中将no-api键设置为true,它将像克隆任何其他Git存储库一样克隆存储库,而不是使用GitHub api。但与直接使用git驱动程序不同,composer仍然会尝试使用GitHub的zip文件。

因此,该部分将如下所示:

"repositories": [
    {
        "type": "vcs",
        "no-api": true,
        "url": "https://github.com/your/repo"
    }
],

请记住,API存在是有原因的。因此,这应该是github.com上增加负载的最后手段。