我从我的GitHub帐户克隆了一个Git存储库到我的电脑。

我想使用我的电脑和笔记本电脑,但只有一个GitHub帐户。

当我尝试使用我的电脑推送到GitHub或从GitHub中拉取时,它需要用户名和密码,但当我使用笔记本电脑时不需要!

我不想每次与origin交互时都键入用户名和密码。我在这里缺少什么?


当前回答

我也有同样的问题。

所以我从项目中更改了.git/config文件,

url = https://github.com/<your-user-here>/<your-repo-here>

to

url = git@github.com:<your-user-here>/<your-repo-here>

并将SSH公钥添加到正在设置的Git配置文件中。

对于SSH公钥:

cat ~/.ssh/id_rsa.pub

其他回答

使用Git存储库永久验证

运行以下命令以启用凭据缓存:

$ git config credential.helper store
$ git push https://github.com/owner/repo.git

Username for 'https://github.com': <USERNAME>
Password for 'https://USERNAME@github.com': <PASSWORD>

您还应该指定缓存过期,

git config credential.helper 'cache --timeout 7200'

启用凭据缓存后,将缓存7200秒(2小时)。43200秒=12小时(每天登录一次)对某些人来说可能也是一个合理的选择。


正如SebastianH所评论的,也可以使用类似的命令来配置,而不是按存储库,而是全局配置:

git config --global credential.helper credential.helper store
git config --global credential.helper 'cache --timeout 7200'

对于Windows Git用户,在运行Git-config--globalcredential.helper store后,如果仍然提示输入密码,最好使用以下命令检查配置文件的写入位置

git config --list --show-origin

在我的例子中,在手动编辑配置文件“C:\Program Files\Git\mingw64\etc\gitconfig”并添加以下文本后,它就工作了。

[credential]
    helper = store

你基本上有两个选择。

如果您在两台计算机上使用同一用户,则需要将.pub密钥复制到PC上,这样GitHub就知道您是同一用户。

如果你已经为你的电脑创建了一个新的.pub文件,并希望将这些机器视为不同的用户,你需要在GitHub网站上注册新的.pub文件。

如果这仍然不起作用,可能是因为ssh配置不正确,ssh无法找到密钥的位置。尝试

ssh -vv username@github.com

获取SSH失败的更多信息。

正如许多用户所说,您只需将Git存储库URL从HTTPS更改为SSH即可。

如果您尚未在计算机中生成SSH密钥,那么您将不得不执行此操作。

作为补充信息,在进行此更改后,我仍然收到相同的错误:

权限被拒绝。

在我的案例中,问题是我使用的是Windows Shell来执行ngh命令;由于该命令应打开一个提示以请求SSH短语,而Windows Shell没有打开这些提示,因此身份验证失败。

所以,我只需要打开Gitshell并在那里执行ngh命令,每次它请求时都在提示符中输入SSH短语,然后“voilà”。。。它工作得很好!

块引用

如果您在请求用户名和密码时遇到$git推送代码问题,请执行以下步骤:

Login to your profile.
Go to settings

click developer settings
->Personal access tokens
--> note: access token
--->Select scopes: checkbox on repo
----> Generate token
-----> Make sure to copy and store it safely if you intend to reuse it.

*** use the generated token as password during pushing code for next 30days ***

***HAPPY CODING***