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

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

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

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


当前回答

步骤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

其他回答

我也有同样的问题。MacOS Mojave钥匙链不断询问密码。为了安全起见,您的id_rsa应该使用密码短语进行加密。然后尝试将其添加到密钥链ssh-add-K~/.ssh/id_rsa

如果密钥位于~/.ssh以外的另一个文件夹中,请用正确的文件夹替换。

Keychain现在知道了你的ssh密钥,希望现在一切正常。

如果您仍然面临这个问题,请尝试

1. brew install keychain

2. echo '/usr/local/bin/keychain $HOME/.ssh/id_rsa' >> ~/.bash_profile
   echo 'source $HOME/.keychain/$HOSTNAME-sh' ~/.bash_profile

3. ssh-add -K ~/.ssh/id_rsa

希望它现在应该起作用了。

更新:-K标志已被弃用。请改用以下方法:

ssh-add --apple-use-keychain ~/.ssh/id_ed25519 # path to private key

步骤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

我想你解决了你的问题,但我看不到能帮我的解决方案,所以就在这里。

键入终端:

echo "" > ~/.ssh/known_hosts

这将清空known_hosts文件,您必须添加您使用过并连接到的每个主机,但这解决了问题。

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时,它不会再消失。

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

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

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