我使用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

其他回答

我认为您可能有错误的Git存储库URL。

打开.git/config并找到[remote“origin”]部分。确保您使用的是SSH:

ssh://git@github.com/username/repo.git

如果单击克隆或下载并选择SSH,您可以在存储库的主页面中看到SSH URL。

而不是https或git:

https://github.com/username/repo.git
git://github.com/username/repo.git

现在,您可以只使用SSH密钥而不是用户名和密码进行验证。

如果Git抱怨已经添加了'origin',请打开.config文件,将[remote origin]后面的url=“…”部分编辑为url=ssh://github/username/repo.git


其他服务也是如此。确保地址如下:protocol://something@网址

例如,Azure DevOps的.git/config:

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

我同意“codehugger”,并使用“orkoden”的指令,它对我有效-在NetBeans 7.3上-当您右键单击文件并选择上下文菜单-push-打开“push to remote”窗口时-这里有两个选项:

来源:https://github.com/myaccount/myproject.git/https://github.com/myaccount/myproject.git/

正如您所看到的,区别在于URL中的原始参数-您不想选择此选项(1),您想检查选项(2),这对我来说很好。

我觉得static_rtti提供的答案在某种意义上很粗糙。我不知道这是否在早期可用,但Git工具现在提供了凭据存储。

缓存模式

$ git config --global credential.helper cache

使用“缓存”模式将凭据保存在内存中一段时间。没有一个密码存储在磁盘上,15分钟后将从缓存中清除。

存储模式

$ git config --global credential.helper 'store --file ~/.my-credentials'

使用“存储”模式将凭据保存到磁盘上的纯文本文件中,它们永远不会过期。

我个人使用的是商店模式。我删除了存储库,克隆了它,然后必须输入一次凭据。

参考:7.14 Git工具-凭证存储

使用以下命令增加超时时间,以便您可以暂时重新键入密码

git config --global credential.helper 'cache --timeout 3600'

我在Bitbucket和GitHub上使用了它,这两个都适用。你唯一需要做的就是3600秒。增加到你想要的程度。我把它改成了259200,大约30天。这样,我每隔30天左右重新输入一次密码。

我也有同样的问题。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