Git - 如何在 Windows 上使用 .netrc 文件来保存用户和密码

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

Git - How to use .netrc file on Windows to save user and password

windowsgitauthentication

提问by Bernd Klaus

Is it possible to use a .netrc file on Windows when I'm using Git to clone a remote repository with HTTP and user - password?

当我使用 Git 克隆带有 HTTP 和用户密码的远程存储库时,是否可以在 Windows 上使用 .netrc 文件?

回答by VonC

Is it possible to use a .netrcfile on Windows?

是否可以.netrc在 Windows 上使用文件?

Yes: You must:

是:您必须:

  • define environment variable %HOME%(pre-Git 2.0, no longer needed with Git 2.0+)
  • put a _netrcfile in %HOME%
  • 定义环境变量%HOME%(Git 2.0 之前的版本,Git 2.0+ 不再需要)
  • 放入一个_netrc文件%HOME%

If you are using Windows 7/10, in a CMDsession, type:

如果您使用的是 Windows 7/10,请在CMD会话中键入:

setx HOME %USERPROFILE%

and the %HOME%will be set to 'C:\Users\"username"'.
Go that that folder (cd %HOME%) and make a file called '_netrc'

并且%HOME%将设置为“ C:\Users\"username"”。
转到那个文件夹 ( cd %HOME%) 并创建一个名为 ' _netrc'的文件

Note: Again, for Windows, you need a '_netrc' file, nota '.netrc' file.

注意:同样,对于 Windows,您需要一个“ _netrc”文件,而不是.netrc”文件。

Its content is quite standard (Replace the <examples>with your values):

它的内容非常标准(<examples>用你的值替换):

machine <hostname1>
login <login1>
password <password1>
machine <hostname2>
login <login2>
password <password2>


Lukementions in the comments:

卢克在评论中提到:

Using the latest version of msysgit on Windows 7, I did not need to set the HOMEenvironment variable. The _netrcfile alone did the trick.

在 Windows 7 上使用最新版本的 msysgit,我不需要设置HOME环境变量。该_netrc文件单独的伎俩。

This is indeed what I mentioned in "Trying to “install” github, .sshdir not there":
git-cmd.batincluded in msysgit does set the %HOME%environment variable:

这确实是我在“试图“ install” github,.sshdir not there
git-cmd.bat中提到的:包含在 msysgit 中确实设置了%HOME%环境变量:

@if not exist "%HOME%" @set HOME=%HOMEDRIVE%%HOMEPATH%
@if not exist "%HOME%" @set HOME=%USERPROFILE%


爱国者 believes in the comments that "it seems that it won't work for http protocol"

爱国者在评论中相信“它似乎不适用于http协议”

However, I answered that netrcis used by curl, and works for HTTP protocol, as shown in this example(look for 'netrc' in the page): . Also used with HTTP protocol here: "_netrc/.netrcalternative to cURL".

但是,我回答说netrc由 使用curl,并且适用于 HTTP 协议,如本示例所示(netrc在页面中查找“ ”): 。此处也与 HTTP 协议一起使用:“ _netrc/.netrc替代cURL”。



A common trap with with netrcsupport on Windows is that git will bypass using it if an origin https url specifies a user name.

netrc在 Windows上支持的一个常见陷阱是,如果源 https url 指定了用户名,git 将绕过使用它。

For example, if your .git/configfile contains:

例如,如果您的.git/config文件包含:

[remote "origin"]
     fetch = +refs/heads/*:refs/remotes/origin/*
     url = https://[email protected]/p/my-project/

Git will not resolve your credentials via _netrc, to fix this remove your username, like so:

Git 不会通过_netrc,解析您的凭据来解决此问题,请删除您的用户名,如下所示:

[remote "origin"]
     fetch = +refs/heads/*:refs/remotes/origin/*
     url = https://code.google.com/p/my-project/


Alternative solution: With git version 1.7.9+(January 2012): This answerfrom Mark Longairdetails the credential cache mechanismwhich alsoallows you to notstore your password in plain text as shown below.

替代方案:用Git版本1.7.9+(2012年1月):这个答案马克Longair详细介绍了凭据缓存机制,可以让你如下图所示明文存储密码。



With Git 1.8.3(April 2013):

使用Git 1.8.3(2013 年 4 月):

You now can use an encrypted .netrc(with gpg).
On Windows: %HOME%/_netrc(_, not '.')

您现在可以使用加密的 .netrc(带有gpg)。
在Windows上:%HOME%/_netrc_而不是' .“)

A new read-only credential helper(in contrib/) to interact with the .netrc/.authinfofiles has been added.

添加了一个新的只读凭据帮助程序(在 中contrib/)以与.netrc/.authinfo文件交互。

That script would allow you to use gpg-encrypted netrc files, avoiding the issue of having your credentials stored in a plain text file.

该脚本将允许您使用 gpg 加密的 netrc 文件,避免将您的凭据存储在纯文本文件中的问题。

Files with the .gpgextension will be decrypted by GPG before parsing.
Multiple -farguments are OK. They are processed in order, and the first matching entry found is returned via the credential helper protocol.

When no -foption is given, .authinfo.gpg, .netrc.gpg, .authinfo, and .netrcfiles in your home directory are used in this order.

带有.gpg扩展名的文件将在解析前由 GPG 解密。
多个-f参数是可以的。它们按顺序处理,找到的第一个匹配条目通过凭证帮助程序协议返回。

当没有-f给出选项,.authinfo.gpg.netrc.gpg.authinfo,和.netrc在你的主目录文件的顺序使用。

To enable this credential helper:

要启用此凭据帮助程序:

git config credential.helper '$shortname -f AUTHFILE1 -f AUTHFILE2'

(Note that Git will prepend "git-credential-" to the helper name and look for it in the path.)

(请注意,Git 将git-credential-在帮助程序名称前添加“ ”并在路径中查找。)

# and if you want lots of debugging info:
git config credential.helper '$shortname -f AUTHFILE -d'

#or to see the files opened and data found:
git config credential.helper '$shortname -f AUTHFILE -v'

See a full example at "Is there a way to skip password typing when using https:// github"

看到一个完整的例子“有没有办法跳过输入密码时,使用https:// github



With Git 2.18+ (June 2018), you now can customize the GPG program used to decrypt the encrypted .netrcfile.

使用 Git 2.18+(2018 年 6 月),您现在可以自定义用于解密加密.netrc文件的 GPG 程序。

See commit 786ef50, commit f07eeed(12 May 2018) by Luis Marsano (``).
(Merged by Junio C Hamano -- gitster--in commit 017b7c5, 30 May 2018)

请参阅Luis Marsano (``) 的commit 786ef50commit f07eeed(2018 年 5 月 12 日
(由Junio C gitsterHamano合并-- --commit 017b7c5,2018 年 5 月 30 日)

git-credential-netrc: accept gpgoption

git-credential-netrcwas hardcoded to decrypt with 'gpg' regardless of the gpg.program option.
This is a problem on distributions like Debian that call modern GnuPG something else, like 'gpg2'

git-credential-netrc: 接受gpg选项

git-credential-netrcgpg无论 gpg.program 选项如何,都被硬编码为使用“ ”解密。
这是 Debian 等发行版的问题,这些发行版将现代 GnuPG 称为其他东西,例如 ' gpg2'

回答by KindDragon

You can also install Git Credential Manager for Windowsto save Git passwords in Windows credentials manager instead of _netrc. This is a more secure way to store passwords.

您还可以安装适用于 Windows 的 Git Credential Manager以将 Git 密码保存在 Windows 凭据管理器而不是_netrc. 这是一种更安全的密码存储方式。

回答by EBlake

This will let Git authenticate on HTTPS using .netrc:

这将使 Git 使用.netrc以下命令在 HTTPS 上进行身份验证:

  • The file should be named _netrcand located in c:\Users\<username>.
  • You will need to set an environment variable called HOME=%USERPROFILE%(set system-wide environment variables using the System option in the control panel. Depending on the version of Windows, you may need to select "Advanced Options".).
  • The password stored in the _netrcfile cannot contain spaces (quoting the password will not work).
  • 该文件应命名_netrc并位于c:\Users\<username>.
  • 您将需要设置一个名为HOME=%USERPROFILE%(使用控制面板中的系统选项设置系统范围的环境变量。根据 Windows 版本,您可能需要选择“高级选项”。)的环境变量。
  • 存储在_netrc文件中的密码不能包含空格(引用密码不起作用)。

回答by BO.LI

I am posting a way to use _netrcto download materials from the site www.course.com.

我正在发布一种用于_netrc从网站 www.course.com 下载材料的方法。

If someone is going to use the coursera-dl to download the open-class materials on www.coursera.com, and on the Windows OS someone wants to use a file like ".netrc" which is in like-Unix OS to add the option -ninstead of -U <username> -P <password>for convenience. He/she can do it like this:

如果有人要使用 coursera-dl 下载 www.coursera.com 上的公开课材料,并且在 Windows 操作系统上有人想使用类似 Unix 操作系统中的“.netrc”文件来添加选项-n而不是-U <username> -P <password>为了方便。他/她可以这样做:

  1. Check the home path on Windows OS: setx HOME %USERPROFILE%(refer to VonC's answer). It will save the HOMEenvironment variable as C:\Users\"username".

  2. Locate into the directory C:\Users\"username"and create a file name _netrc.NOTE: there is NOT any suffix.the content is like: machine coursera-dl login <user> password <pass>

  3. Use a command like coursera-dl -n --path PATH <course name>to download the class materials. More coursera-dl options details for this page.

  1. 检查 Windows 操作系统上的主路径:(setx HOME %USERPROFILE%请参阅VonC 的回答)。它将HOME环境变量保存为C:\Users\"username".

  2. 定位到目录C:\Users\"username"并创建一个文件名_netrc注意:没有任何后缀。内容是这样的:machine coursera-dl login <user> password <pass>

  3. 使用类似命令coursera-dl -n --path PATH <course name>下载课程资料。此页面的更多 coursera-dl 选项详细信息。