从 GitHub 通过 https 使用双因素身份验证进行 Git Clone

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31305945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:22:49  来源:igfitidea点击:

Git Clone from GitHub over https with two-factor authentication

gitauthenticationgithub

提问by peter-b

I recently began using two-factor authentication on GitHub, and I am now unable to use git over https on private repos in the usual way:

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

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/'

If I disable two-factor authentication I can use it as before:

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

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.

I know I can use SSH and everything works, but is there a way I can keep two-factor authentication while still being able to use GitHub over HTTPS, for example by sending an auth token with my request?

我知道我可以使用 SSH 并且一切正常,但是有没有一种方法可以保持两因素身份验证,同时仍然可以通过 HTTPS 使用 GitHub,例如通过随我的请求发送身份验证令牌?

回答by Nitsew

Find out how to fix this here:

在此处了解如何解决此问题:

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

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

How does it work for command-line Git?

If you are using SSH for Git authentication, rest easy: you don't need to do anything. If you are using HTTPS Git, instead of entering your password, enter a personal access token. These can be created by going to your personal access tokens page.

它如何用于命令行 Git?

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

回答by Sunil Targe

As per @Nitsew'sanswer, Create your personal access tokenand use your token as your username and enter with blank password.

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

Later you won't need any credentials to access all your private repo(s).

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

回答by Zvone

To everyone struggling, what worked for me was creating personal access token and then using it as a username AND password(in the prompt that opened).

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

回答by Richard

If your repo have 2FA enabled. Highly suggest to use the app provided by github.com Here is the link: https://desktop.github.com/

如果您的存储库启用了 2FA。强烈建议使用 github.com 提供的应用程序链接如下:https://desktop.github.com/

After you downloaded it and installed it. Follow the withard, the app will ask you to provide the one time password for login. Once you filled in the one time password, you could see your repo/projects now.

下载并安装后。按照withard,该应用程序会要求您提供登录的一次性密码。填写一次性密码后,您现在可以看到您的回购/项目。

回答by hiroakit

1st: Get personal access token. https://github.com/settings/tokens
2nd: Put account & the token. Example is here:

第一:获取个人访问令牌。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