Git Clone来自GitHub,通过https进行双因素身份验证

pet*_*r-b 100 git authentication github

我最近开始在GitHub上使用双因素身份验证,我现在无法以通常的方式在私有存储库上使用git over https:

peter@computer:~$ git clone https://github.com/[...]/MyPrivateRepo
Cloning into 'MyPrivateRepo'...
Username for 'https://github.com': [...]
Password for 'https://[...]@github.com': 
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/[...]/MyPrivateRepo/'
Run Code Online (Sandbox Code Playgroud)

如果我禁用双因素身份验证,我可以像以前一样使用它:

peter@computer:~$ git clone https://github.com/[...]/MyPrivateRepo
Cloning into 'MyPrivateRepo'...
Username for 'https://github.com': [...]
Password for 'https://[...]@github.com': 
remote: Counting objects: 147, done.
remote: Total 147 (delta 0), reused 0 (delta 0), pack-reused 147
Receiving objects: 100% (147/147), 22.70 KiB | 0 bytes/s, done.
Resolving deltas: 100% (87/87), done.
Checking connectivity... done.
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用SSH,一切正常,但有没有办法可以保持双因素身份验证,同时仍然能够通过HTTPS使用GitHub,例如通过发送带有我请求的身份验证令牌?

Nit*_*sew 112

在这里了解如何解决这个问题:

https://github.com/blog/1614-two-factor-authentication#how-does-it-work-for-command-line-git

它如何用于命令行Git?

如果您使用SSH进行Git身份验证,请轻松休息:您无需执行任何操作.如果您使用的是HTTPS Git,请输入个人访问令牌,而不是输入密码.这些可以通过访问您的个人访问令牌页面来创建.

  • 使用您的令牌作为您的用户名并输入空白密码. (21认同)
  • 您可以按照以下[要点](https://gist.github.com/wikimatze/9790374#use-netrc-file-to-store-credentials)中的说明,将令牌添加到.netrc文件中。也是自动的。 (2认同)
  • 在sudo git clone之后输入mac中的访问令牌后仍然无法为我工作....系统要求在密码字段中输入用户名和密码我输入令牌但它不适合我.得到同样的错误 (2认同)

Sun*_*rge 19

根据@Nitsew的回答,创建您的个人访问令牌并使用您的令牌作为您的用户名并输入空白密码.

稍后您将不需要任何凭据来访问您的所有私人仓库.


小智 10

对于每个苦苦挣扎的人来说,对我有用的是创建个人访问令牌,然后将其用作用户名和密码(在打开的提示中)。


Ser*_*eis 7

通常会想到您已经设置了两因素身份验证,经过几次密码尝试和密码重置。那么,我们如何使用双因素身份验证来 git 克隆私有存储库呢?很简单,使用访问令牌。

如何使用访问令牌对 Git 进行身份验证

  1. 转到https://github.com/settings/tokens
  2. 单击右上角的生成新令牌按钮。
  3. 为您的令牌指定一个描述性名称。
  4. 为令牌设置所有必需的权限。
  5. 单击底部的生成令牌按钮。
  6. 将生成的令牌复制到安全的地方。
  7. 使用 git clone 时使用此令牌而不是密码。

哇,它有效!


小智 5

第一:获取个人访问令牌。https://github.com/settings/tokens
第二:放置帐户和令牌。例子在这里:

$ git push
Username for 'https://github.com':            # Put your GitHub account name
Password for 'https://{USERNAME}@github.com': # Put your Personal access token
Run Code Online (Sandbox Code Playgroud)

有关如何创建个人访问令牌的链接:https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line