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

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

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

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


当前回答

这就是对我有用的:

git remote set-url origin https://username@github.com/username/reponame.git

例子:

git remote set-url origin https://jsmith@github.com/jsmith/master.git

其他回答

如果您在Github帐户上启用了2FA,那么您的常规密码将无法用于此目的,但您可以生成一个个人访问令牌并将其替换为使用。

访问GitHub中的设置->开发人员设置->个人访问令牌页面(https://github.com/settings/tokens/new),并生成具有所有Repo权限的新令牌:

然后页面将显示新的令牌值。保存此值,并在推送到GitHub上的存储库时使用它代替密码:

> git push origin develop
Username for 'https://github.com': <your username>
Password for 'https://<your username>@github.com': <your personal access token>

来源:设置Git

以下命令将在内存中保存密码一段时间(对于Git 1.7.10或更高版本)。

$ git config --global credential.helper cache
# Set git to use the credential memory cache

$ git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after one hour (setting is in seconds)

使用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(例如,GitBash)(如果您不想从HTTPS切换到SSH),也可以使用Windows的Git凭据管理器

此应用程序将为您保留用户名和密码。。。

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

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

而不是这样:

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

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