我从我的GitHub帐户克隆了一个Git存储库到我的电脑。
我想使用我的电脑和笔记本电脑,但只有一个GitHub帐户。
当我尝试使用我的电脑推送到GitHub或从GitHub中拉取时,它需要用户名和密码,但当我使用笔记本电脑时不需要!
我不想每次与origin交互时都键入用户名和密码。我在这里缺少什么?
我从我的GitHub帐户克隆了一个Git存储库到我的电脑。
我想使用我的电脑和笔记本电脑,但只有一个GitHub帐户。
当我尝试使用我的电脑推送到GitHub或从GitHub中拉取时,它需要用户名和密码,但当我使用笔记本电脑时不需要!
我不想每次与origin交互时都键入用户名和密码。我在这里缺少什么?
当前回答
HTTPS更新:
GitHub为Windows推出了一个新程序,在您使用HTTPS时存储您的凭据:
要使用:
从此处下载程序运行程序后,它将编辑.gitconfig文件。重新检查它是否编辑了正确的.gitconfig,以防出现多个错误。如果它没有编辑正确的文件,请将以下内容添加到.gitconfig中[凭据]助手=!'C: \\Path\\To\\Your\\Downloaded\\File\\git credential winstore.exe'注意[credential]后面的换行符。这是必需的。打开命令行客户端,尝试git push origin master一次。如果它要求您输入密码,请输入密码,您就完成了。密码已保存!
其他回答
如果您在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 remote set-url origin https://username@github.com/username/reponame.git
例子:
git remote set-url origin https://jsmith@github.com/jsmith/master.git
使用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密钥或.netrc文件不适用于您,那么另一个简单但不太安全的解决方案可能适用于您:git credential store-Helper将凭据存储在磁盘上:
git config --global credential.helper store
默认情况下,凭据将保存在文件~/.git凭据中。它将被创建并写入。
请注意,使用此助手将在磁盘上存储未加密的密码,仅受文件系统权限的保护。如果这可能不是可接受的安全权衡。