我使用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 remote add origin master "https://...git " 

在Windows for Git 1.7.9+中,在命令提示符下运行以下命令以在文本编辑器中打开配置文件:

git config --global --edit

然后在文件中,添加以下块(如果不存在)或相应地进行编辑:

[credential "https://giturl.com"]
username = <user id>
helper = wincred

保存并关闭文件。在上述更改后,您只需提供一次凭据。

如果设置了SSH代理,还可以将其添加到~/.gitconfig中,以强制git对所有GitHub repo使用SSH,而不是HTTPS:

[url "ssh://git@github.com/"]
    insteadOf = git://github.com/
    insteadOf = https://github.com/

(如果你主要使用公共回购,你也可以使用pushInsteadOf而不是insteadOf,因为从公共回购中读取可以在没有身份验证的情况下完成)。

git config credential.helper store

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

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

git config --unset credential.helper

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

git config --global credential.helper store

如果你想阻止Git总是要求你提供GitHub存储库的登录凭据,这很容易做到。

使用SSH而不是HTTPS

您可以使用SSH而不是HTTPS更新源远程“

git remote set-url origin git@github.com:username/your-repo.git

配置Git以存储密码和用户名

下面是如何让Git存储用户名和密码:

git config --global credential.helper store

接下来,保存会话的用户名和密码:

git config --global credential.helper cache