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

有没有办法缓存凭据,而不是每次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

其他回答

如果你像我一样使用双因素身份验证,情况就有点不同了。因为我在其他地方找不到好答案,所以我会在这里留下一个,这样也许我可以稍后找到它。

如果您使用的是双因素身份验证,那么指定用户名/密码甚至不起作用——您的访问被拒绝。但您可以使用应用程序访问令牌,并使用Git的凭据助手为您缓存该令牌。以下是相关链接:

设置命令行以使用2-factor auth(搜索标题为“它如何为命令行Git工作?”的部分)凭据缓存

我不记得我是在哪里看到的,但当你被要求输入用户名时,你就会在那里粘贴应用程序访问令牌。然后将密码留空。它在我的Mac上运行。

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

报价:

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

下载二进制git凭证osxkeychain。

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

chmod a+x git-credential-osxkeychain

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

运行以下命令:

git config --global credential.helper osxkeychain

您还可以让Git使用Git凭据存储永久存储您的凭据,如下所示:

git config credential.helper store

注意:虽然这很方便,但Git会在项目目录下的本地文件(.git凭据)(参见下面的“home”目录)。如果不喜欢,请删除此文件并切换到使用缓存选项。

如果您希望Git恢复到每次需要时都向您请求凭据连接到远程存储库,可以运行以下命令:

git config --unset credential.helper

要将密码存储在%HOME%目录(而不是项目目录)中的.git凭据中,请使用--global标志

git config --global credential.helper store

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

通常你有一个远程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)