将 Git 客户端(如 Windows 版 GitHub)配置为不要求身份验证

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

Configure Git clients, like GitHub for Windows, to not ask for authentication

gitgithubgithub-for-windows

提问by prongs

I have installed GitHub for Windows and also GitExtensions and have multiple versions of git.exe in my path.

我已经为 Windows 安装了 GitHub 和 GitExtensions,并且在我的路径中有多个版本的 git.exe。

enter image description here

在此处输入图片说明

C:\Users\Rajat\AppData\Local\GitHub\PortableGit_93e8418133eb85e81a81e5e19c272776524496c6\cmd\git.exe
C:\Users\Rajat\AppData\Local\GitHub\PortableGit_93e8418133eb85e81a81e5e19c272776524496c6\bin\git.exe
E:\cygwin\bin\git.exe
C:\Program Files (x86)\Git\cmd\git.exe
C:\Program Files (x86)\Git\bin\git.exe

Now, when I do git push origin masterwith any of the last three git.exes, it asks for my username. But the Portable Git doesn't ask for username. See the following screenshot:

现在,当我git push origin master使用最后三个git.exes 中的任何一个时,它会询问我的用户名。但是便携式 Git 不要求输入用户名。请参阅以下屏幕截图:

enter image description here

在此处输入图片说明

The heart-shaped character is just a ^Cso ignore that.

心形字符只是一个^C忽略。

How is authentication being taken care of in this case? Ultimately I want the last three Gits to not ask for authorization. How's that possible?

在这种情况下如何处理身份验证?最终我希望最后三个 Git 不要求授权。这怎么可能?

I found two extra files in the GitHub's Git but I doubt they matter at all:

我在 GitHub 的 Git 中发现了两个额外的文件,但我怀疑它们根本不重要:

enter image description here

在此处输入图片说明

回答by Ana Betts

If you're using GitHub for Windows, all you need to do is:

如果您使用 GitHub for Windows,您需要做的就是:

  1. Make sure you've logged into the GUI app at least once
  2. Click the "Git Shell" link
  1. 确保您至少登录过一次 GUI 应用程序
  2. 单击“Git Shell”链接

GitHub has already solved this problem for you on the command line, for both HTTPS and SSH-based remotes

GitHub 已经在命令行上为你解决了这个问题,无论是基于 HTTPS 还是基于 SSH 的远程

回答by VonC

For GitHub for Windows itself, Paul Betts(GitHub staff) gently reminds everyone that G4W already includes a credential helper(based on CryptProtectand I suppose the CryptProtectDatafunction)
See his answer below.

对于 GitHub for Windows 本身,Paul BettsGitHub 工作人员)温和地提醒大家,G4W 已经包含了一个凭证助手(基于CryptProtect和我假设的CryptProtectData功能
下面他的回答

For those who don't use G4W, read on.

对于那些不使用 G4W 的人,请继续阅读。



To add to my previous answer(when pushing to GitHub through the console/command-line):

添加到我之前的答案(通过控制台/命令行推送到 GitHub 时):

A file like ~/.netrc(on Unix) or %HOME%/_netrc(on Windows) can help you to avoid entering your credential for every git push you would make to GitHub.

~/.netrc(在 Unix 上)或%HOME%/_netrc(在 Windows 上)这样的文件可以帮助您避免在每次 git push 时输入您的凭据到 GitHub

You can store as many credentials you need in a netrcfile.
(for GitHub or other repo hosting providers)
But:

您可以在一个netrc文件中存储任意数量的凭据。
(对于 GitHub 或其他 repo 托管提供商)
但是:

  • you don't want to store your main GitHub password account
  • you don't want those in a plain text file.
  • 您不想存储您的主要 GitHub 密码帐户
  • 你不希望那些在纯文本文件中。

The following sections address those issues:

以下部分解决了这些问题:



Enable the two-factor authentication (2FA) on your GitHub account

在您的 GitHub 帐户上启用双因素身份验证 (2FA)

(September 3rd, 2013)

(2013 年 9 月 3 日)

Configure 2FA through an application, always through an app, neverthrough a text SMS, if you can avoid it.

如果可以避免的话,通过应用程序配置 2FA,始终通过应用程序,从不通过文本 SMS。

The reason is, through that activation process, you have access to your two-factor secret key, which is used to generate the second factor authentication every 30 seconds:

原因是,通过该激活过程,您可以访问双因素密钥,该密钥用于每 30 秒生成一次第二因素身份验证:

two factor secret string

两因素秘密字符串

That way you can go to any GAuth (Google Authenticator) client, enter that same 2FA secret key, and see the exactsame code you would get through SMS message.
Except that, if you don't have phone service, it still works ;)

这样您就可以转到任何 GAuth(Google 身份验证器)客户端,输入相同的 2FA 密钥,然后查看与通过 SMS 消息获得的完全相同的代码。
除此之外,如果您没有电话服务,它仍然有效;)

Of course, the first client to use is GAuthon your android phone or your iphone.

当然,第一个使用的客户端是您的 android 手机或 iphone 上的GAuth

GAuth on Android

Android 上的 GAuth

That means you don't have to wait for an SMS, and you keep your 2FA on your phone.

这意味着您不必等待短信,并且您可以将 2FA 保存在手机上。

However, having your secret key allows you to not be limited to a client on your phone.
You can go to any other client, like:

但是,拥有您的密钥可以让您不受手机上的客户端的限制。
您可以转到任何其他客户端,例如:

For all those clients (on your phone with GAuth, or with a desktop client or a web page), you will need your two-factor secret key.

对于所有这些客户端(在您的手机上使用 GAuth,或使用桌面客户​​端或网页),您将需要您的双因素密钥

If you activated your 2FA through SMS:

如果您通过短信激活了 2FA:

  • you don't know your secret key
  • you can't use any other GAuth client
  • you are limited to receiving your token through your phone (if you have phone service and/or if you have your phone at all)
  • 你不知道你的密钥
  • 您不能使用任何其他 GAuth 客户端
  • 您只能通过手机接收令牌(如果您有电话服务和/或如果您有手机)


Note: if you have added a key in your Gauth client on Android, without having memorized first said secret key, all is not lost.
(but you need a rooted phone though)

注意:如果您在 Android 上的 Gauth 客户端中添加了一个密钥,而没有记住第一个所说的密钥,那么一切都不会丢失
(但你需要一个root手机)

$ adb shell
# sqlite3 /data/data/com.google.android.apps.authenticator/databases/databases
sqlite> select * from accounts;
1|[email protected]|your2factorkey|0|0
sqlite> .quit
#exit


Don't forget to get and then save the associated recovery codes (in the Account Settingssection of your GitHub account):

不要忘记获取并保存相关的恢复代码(在Account Settings您的 GitHub 帐户部分):

Recovery code

恢复码

(See also the last section about whereto save those codes)

(另请参阅关于在哪里保存这些代码的最后一节)



Encrypt your _netrcfile

加密你的_netrc文件

(see the credential helper netrc with git1.8.3+: gpg encryption)

(参见带有 git1.8.3+凭证助手 netrc:gpg 加密)

You need to encrypt in that file at least those two credentials:

您需要在该文件中至少加密这两个凭据:

machine github.com
login username
password xxxx
protocol https

machine gist.github.com
login username
password xxxx
protocol https

You then keep only a ~/.netrc.gpgor %HOME%/_netrc.gpg

然后你只保留一个~/.netrc.gpg%HOME%/_netrc.gpg

But, if you enable the new two-factor authentication described above, 'xxxx' won't be your GitHub account: see the next section about "Personal Access Token".

但是,如果您启用上述新的双因素身份验证,“ xxxx”将不会是您的 GitHub 帐户:请参阅下一节有关“个人访问令牌”的内容。



Generate a Personal Access Token

生成个人访问令牌

You won't be able to push with your GitHub password if you have activated 2FA.

如果您已激活 2FA,您将无法使用 GitHub 密码进行推送。

Anonymous access to user/repo.git denied

Anonymous access to user/repo.git denied

Here is what you would see (the gpgpart is because I use the netrccredential helper):

这是您将看到的(gpg部分是因为我使用了netrc凭证助手):

C:\Users\VonC\prog\git\git>git push origin
Using GPG to open %HOME%/_netrc.gpg: [gpg2 --decrypt %HOME%/_netrc.gpg]

You need a passphrase to unlock the secret key for
user: "auser <[email protected]>"
2048-bit RSA key, ID A2EF56, created 2012-09-12 (main key ID DC43D6)

remote: Anonymous access to VonC/git.git denied.                      <=====
fatal: Authentication failed for 'https://[email protected]/VonC/git/'  <=====

So go to the Developersection of your GitHub Account(subsection Personal access tokens), and generate a Personal Access Token:

因此,转到Developer您的 GitHub 帐户部分(子部分Personal access tokens),并生成个人访问令牌

Personal token

个人令牌

That token won't require a two-factor authentication: you can use it as password in your _netrcfile, and you will be able to just push to GitHub.

该令牌不需要双重身份验证:您可以将其用作_netrc文件中的密码,并且您只需推送到 GitHub。

But the difference with your main GitHub account password is:
You can revoke a personal access token(and generate a new one), while still keeping your main password unchanged.

但与您的主 GitHub 帐户密码不同的是:
您可以撤销个人访问令牌(并生成一个新的),同时仍保持您的主密码不变。

If you had encrypted your main GitHub password in your ~/.netrc.gpgfile, you can replace it with your new personal token:

如果你在你的~/.netrc.gpg文件中加密了你的主 GitHub 密码,你可以用你的新个人令牌替换它:

gpg -d %HOME%\_netrc.gpg | sed "s/yourPassord/YourPersonalAccessToken/g" | gpg -e -r auser --yes -o %HOME%\_netrc.gpg

In multiple line for readability:

多行以提高可读性:

gpg -d %HOME%\_netrc.gpg | 
  sed "s/yourPassord/YourPersonalAccessToken/g" | 
  gpg -e -r auser --yes -o %HOME%\_netrc.gpg

That works even on Windows, with the help of the unix-like GoW (Gnu on Windows)command, which includes sed.

在类 Unix 的GoW(Windows 上的 Gnu)命令的帮助下,它甚至在 Windows上也能工作,其中包括sed.



Save your GitHub credentials

保存您的 GitHub 凭据

I recommend an online credential storage like lastpass.com

我推荐像lastpass.com这样的在线凭证存储

You need to save:

您需要保存:

  • Your GitHub account password
  • Your 2FA secret key
  • Your 2FA recovery code
  • Your personal token
  • 您的 GitHub 帐户密码
  • 您的 2FA 密钥
  • 您的 2FA 恢复代码
  • 您的个人令牌

LastPass

最后通行证

回答by prongs

https://help.github.com/articles/set-up-git

https://help.github.com/articles/set-up-git

The password caching section says:

密码缓存部分说:

If you don't want to use GitHub for Windows, you can download the helper for your OS here:

如果您不想在 Windows 上使用 GitHub,可以在此处下载适用于您的操作系统的帮助程序:

Do that and any other git.exe will work without asking for password. :)

这样做,任何其他 git.exe 都可以在不要求密码的情况下工作。:)