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

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


当前回答

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上增加负载的最后手段。

其他回答

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上增加负载的最后手段。

我知道这不是一个安全的解决方案,但有时你只需要一个简单的解决方案-而不需要安装任何其他东西。由于helper=store不适合我,我创建了一个虚拟助手:

创建一个脚本并将其放在用户bin文件夹中,此处名为credfake,该脚本将提供您的用户名和密码:

#!/bin/bash
while read line
do
  echo "$line"
done < "/dev/stdin"
echo username=mahuser
echo password=MahSecret12345

使其可执行:

chmod u+x /home/mahuser/bin/credfake

然后在git中配置它:

git config --global credential.helper /home/mahuser/bin/credfake

(或不使用--仅限一个回购协议的全局)

和-voilá-git将使用此用户+密码。

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

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

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

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

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

通常你有一个远程URL,像这样,

git remote -v

origin    https://gitlab.com/username/Repo.git (fetch)
origin    https://gitlab.com/username/Repo.git (push)

如果您想在使用git push时跳过用户名和密码,请尝试以下操作:

 git remote set-url origin https://username:password@gitlab.com/username/Repo.git

我刚刚向origin添加了相同的URL(包括密码在内的用户详细信息)。

注意:如果用户名是电子邮件Id,则不起作用。

git remote -v

origin    https://username:password@gitlab.com/username/Repo.git (fetch)
origin    https://username:password@gitlab.com/username/Repo.git (push)

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

报价:

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

下载二进制git凭证osxkeychain。

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

chmod a+x git-credential-osxkeychain

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

运行以下命令:

git config --global credential.helper osxkeychain