git 2FA 推送到 GitHub 时出现问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40950497/
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-19 12:28:24  来源:igfitidea点击:

2FA give problems when pushing to GitHub

gitgithubgit-pushtwo-factor-authentication

提问by H. Pauwelyn

I've clone a project on GitHub on my Raspberry Pi, create a new branch and push everything to the repository. For this I needed next commands:

我在 Raspberry Pi 上的 GitHub 上克隆了一个项目,创建了一个新分支并将所有内容推送到存储库。为此,我需要下一个命令:

git clone https://www.github.com/heinpauwelyn/my_repo
git checkout -b raspberry
git push origin raspberry

The problem I've got is that I can't push the branch to GitHub.com. I need to enter my username and password, but I can't use 2FA for that. Is this a bug in Git or GitHub and is there a way to get an authentication key and enter it?

我遇到的问题是我无法将分支推送到 GitHub.com。我需要输入我的用户名和密码,但我不能为此使用 2FA。这是 Git 或 GitHub 中的错误,有没有办法获取身份验证密钥并输入它?

I'll not enable the 2FA on GitHub.

我不会在 GitHub 上启用 2FA。

回答by pedrorijo91

with 2FA you have to create a personal access token to use as a password when authenticating to GitHub on the command line with HTTPS URLs: https://help.github.com/articles/which-remote-url-should-i-use/#when-2fa-is-enabled

使用 2FA,您必须创建一个个人访问令牌,以便在使用 HTTPS URL 在命令行上对 GitHub 进行身份验证时用作密码:https: //help.github.com/articles/which-remote-url-should-i-use /#when-2fa-is-enabled

or you can clone with ssh https://help.github.com/articles/which-remote-url-should-i-use/#cloning-with-ssh-urls(may also be useful: https://help.github.com/articles/generating-an-ssh-key/)

或者您可以使用 ssh 进行克隆https://help.github.com/articles/which-remote-url-should-i-use/#cloning-with-ssh-urls(也可能有用:https://help. github.com/articles/generate-an-ssh-key/)

回答by Carlos Giovani Barillas Colón

You have to generate an access token and use the access token instead the password. For example:

您必须生成访问令牌并使用访问令牌而不是密码。例如:

$ git clone https://github.com/username/repo.git
Username: your_username
Password: your_token

Doc: https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line

文档:https: //help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line

回答by Umar Asghar

with 2FA you need to generate personal access token while pushing the code. That personal token will be used as a password while pushing the code to Github. You can see that how to create the personal access token from https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line. It will be used when you have clone the repo with http url. It will be working for all the repositories.

使用 2FA,您需要在推送代码时生成个人访问令牌。在将代码推送到 Github 时,该个人令牌将用作密码。您可以从https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line看到如何创建个人访问令牌。当您使用 http url 克隆 repo 时将使用它。它将适用于所有存储库。

If you have cloned the through SSH then you can push your changing very easily without any changes in normal behavior after enabling the 2F authentication. For this you use SSH key passphrase as a password. First it requires you to create SSH key against your repository. You can generate SSH key from https://help.github.com/en/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agentIt will be working only single repository only for that specific repsority again which this SSH key has been generated.

如果您已经通过 SSH 克隆了 SSH,那么在启用 2F 身份验证后,您可以非常轻松地推送您的更改,而不会对正常行为进行任何更改。为此,您使用 SSH 密钥密码作为密码。首先,它要求您针对您的存储库创建 SSH 密钥。您可以从https://help.github.com/en/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent生成 SSH 密钥它将仅适用于单个存储库仅针对已生成此 SSH 密钥的特定报告。

回答by CoderSam

You can either use HTTPS URL of the repo or SSH URL of the for pushing, pulling, cloning or fetching operations from your local server after you have added 2FA on your GitHub account. The difference will be:

在您的 GitHub 帐户上添加 2FA 后,您可以使用 repo 的 HTTPS URL 或 SSH URL 从本地服务器进行推送、拉取、克隆或获取操作。区别将是:

While using the HTTPS URL: Now for pushing, pulling, fetching or cloning operations, you have to generate a Personal access tokenform your GitHub account and that will be used as password whenever you are asked for a password. You have to keep the token secure.

使用 HTTPS URL 时:现在对于推送、拉取、获取或克隆操作,您必须从您的 GitHub 帐户生成个人访问令牌,每当您被要求输入密码时,该令牌将用作密码。您必须确保令牌安全。

Visit: Creating a personal access token for the command line

访问:为命令行创建个人访问令牌

While using the SSH URL: For Pushing, Pulling, Fetching or Cloning through SSH URL of the repo, you need to have a private key and public pair set up for your account. This will take a little amount of time but once you are done setting your private and public key, you will never be prompted for a username or password because now GitHub knows your identity.

使用 SSH URL 时:对于通过 repo的 SSH URL推送、拉取、获取或克隆,您需要为您的帐户设置私钥和公共对。这将需要一些时间,但是一旦您完成设置您的私钥和公钥,您将永远不会被提示输入用户名或密码,因为现在 GitHub 知道您的身份。

For creating the private key and public key pair, read: Connecting to GitHub with SSH

要创建私钥和公钥对,请阅读:使用 SSH 连接到 GitHub

Difference between using HTTPS URL and SSH URL:

使用 HTTPS URL 和 SSH URL 的区别

While HTTPS is not blocked by any firewall or by any network, SSH may be blocked sometimes and you may not be able to use it, however, it happens rarely. While using HTTPS, as said you will be asked ofter for username and password(which is your personal access token), you can cache it using credential.helperbut it will save as a plain text.

虽然 HTTPS 不会被任何防火墙或任何网络阻止,但有时 SSH 可能会被阻止并且您可能无法使用它,但是这种情况很少发生。在使用 HTTPS 时,如上所述,您会被要求提供用户名和密码(这是您的个人访问令牌),您可以使用credential.helper它缓存它,但它会保存为纯文本。

While for SSH, you can generate a passphrase for your private key, How do I add a password to an OpenSSH private key that was generated without a password?

而对于 SSH,您可以为您的私钥生成密码,如何将密码添加到没有密码生成的 OpenSSH 私钥?

Now your private key will be secured but whenever you'll Push, Pull, Clone or Fetch, the passphrase will be asked each time. To avoid that you can use an SSH agent, SSH Key - Still asking for password and passphrase

现在您的私钥将受到保护,但是每当您执行推送、拉取、克隆或获取时,每次都会询问密码。为避免这种情况,您可以使用 SSH 代理、SSH 密钥 - 仍然要求输入密码和密码

回答by J.Z.

If you've cloned over https and want to keep using that, for whatever reason, you can edit .git/configto include the personal access token generated per https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line.

如果您已经通过 https 克隆并希望继续使用它,无论出于何种原因,您都可以编辑.git/config以包含根据https://help.github.com/en/github/authenticating-to-github/creating生成的个人访问令牌-a-personal-access-token-for-the-command-line

A sample .git/configentry:

示例.git/config条目:

[remote "<YOUR-REMOTE-NAME>"]
    url = https://<YOUR-USERNAME>:<YOUR-TOKEN>@github.com/<etc your repo url>

This is putting your token key in this plain text file on your machine, which is bad, but if you need a quick hack to get things going, it works.

这是将您的令牌密钥放在您机器上的这个纯文本文件中,这很糟糕,但是如果您需要快速破解才能使事情顺利进行,它可以工作。

Cheers!

干杯!