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

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

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

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


当前回答

你基本上有两个选择。

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

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

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

ssh -vv username@github.com

获取SSH失败的更多信息。

其他回答

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

块引用

如果您在请求用户名和密码时遇到$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***

我刚刚遇到了同样的问题,我找到的最简单的解决方案是使用SSH URL而不是HTTPS URL:

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

而不是这样:

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

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

您可以在Git中缓存GitHub密码:

只需遵循GitHub官方文档中的说明即可。

按照上述链接中的说明进行操作后,您应该可以在不每次输入用户名/密码的情况下,向存储库推送/从存储库中推送。

还有一个选项:

而不是写作

git push origin HEAD

你可以写:

git push https://user:pass@yourrepo.com/path HEAD

显然,对于大多数shell,这将导致密码被缓存在历史中,所以请记住这一点。