我使用Git已经有一段时间了,但不断的密码请求开始让我感到很烦。

我使用的是Mac OS X和GitHub,我按照GitHub的设置Git页面的指示设置Git和SSH密钥。

我还将github SSH密钥添加到了我的Mac OS X密钥链中,正如github的SSH密钥密码页面中提到的那样。我的公钥已在Git中注册。

然而,每次我尝试Git拉时,我都必须输入用户名和密码。除了SSH密钥之外,我是否需要为此设置其他东西?


当前回答

在Windows Subsystem for Linux(WSL)上,这是我发现的唯一可行的解决方案:

eval `ssh-agent`; ssh-add ~/.ssh/id_rsa

这是ssh代理未在WSL中正确注册的问题。

其他回答

Microsoft Stack解决方案(Windows和Azure DevOps)

首先打开.git/config文件,确保地址如下:

protocol://something@url

例如,Azure DevOps的.git/config:

[remote "origin"]
    url = https://mystore@dev.azure.com/mystore/myproject/
    fetch = +refs/heads/*:refs/remotes/origin/*

如果问题仍然存在,请打开Windows凭据管理器,单击名为Windows凭据的安全框并删除所有与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

运行macOS Cataline 10.15时,密钥链缓存方法对我不起作用

以下是对我有用的:

git remote rm origin

git remote add origin https://your_git_username:your_git_password@github.com/path_to_your_git.git

这也应该在GitLab上运行

确保用户名包含电子邮件地址,以删除@email部分,否则您将收到一个错误,说明URL使用了错误/非法格式或缺少URL。

希望这有帮助!

步骤1:检查当前配置

cat .git/config

您将获得:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = https://github.com/path_to_your_git.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[user]
    name = your_username
    email = your_email
[branch "master-staging"]
    remote = origin
    merge = refs/heads/master-staging

步骤2:删除远程源

git remote rm origin

步骤3:使用用户名和密码添加远程源

git remote add origin https://your_git_username:your_git_password@github.com/path_to_your_git.git

在windows机器中:安装GitHub CLI

choco install gh

然后运行命令:

gh auth login

并遵循说明

您也可以点击此链接:

https://cli.github.com/manual/gh_auth_login