Git - 如何在 Windows 上使用 .netrc 文件来保存用户和密码 [英] Git - How to use .netrc file on Windows to save user and password

查看:82
本文介绍了Git - 如何在 Windows 上使用 .netrc 文件来保存用户和密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

是:您必须:

  • 定义环境变量 %HOME%(Git 2.0 之前的版本,Git 2.0+ 不再需要)
  • %HOME%中放入一个_netrc文件

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

setx HOME %USERPROFILE%

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

的文件

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

它的内容非常标准(将 替换为您的值):

machine 登录<登录1>密码 <password1>机器 登录 密码<password2>

<小时>

卢克在评论中提到:

<块引用>

在 Windows 7 上使用最新版本的 msysgit,我不需要设置 HOME 环境变量._netrc 文件本身就起到了作用.

这确实是我在尝试到install"github,.ssh dir not there":
git-cmd.bat 包括在 msysgit 中确实设置了 %HOME% 环境变量:

@if 不存在 "%HOME%" @set HOME=%HOMEDRIVE%%HOMEPATH%@if 不存在 "%HOME%" @set HOME=%USERPROFILE%

<小时>

爱国者相信评论它似乎不适用于http协议"

然而,我回答说netrccurl使用,并且适用于HTTP协议,如这个例子(寻找'netrc'在页面中):.此处也与 HTTP 协议一起使用:_netrc/.netrc 替代 cURL".

<小时>

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

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

<前>[远程起源"]fetch = +refs/heads/*:refs/remotes/origin/*网址 = https://bob@code.google.com/p/my-project/

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

<前>[远程起源"]fetch = +refs/heads/*:refs/remotes/origin/*网址 = https://code.google.com/p/my-project/

<小时>

替代解决方案:使用 git 版本 1.7.9+(2012 年 1 月):这个答案 来自 Mark Longair 详细介绍了 凭据缓存机制允许您以明文形式存储密码文字如下图.

<小时>

使用 Git 1.8.3(2013 年 4 月):

您现在可以使用加密的 .netrc(带有 gpg).
在 Windows 上:%HOME%/_netrc(_,不是 '.')

<块引用>

A 新的只读添加了凭证助手(在contrib/中)与.netrc/.authinfo文件交互.

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

<块引用>

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

当没有给出-f选项时,.authinfo.gpg.netrc.gpg.authinfo.netrc 文件在您的主目录中按此顺序使用.

要启用此凭据帮助程序:

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

<块引用>

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

# 如果你想要很多调试信息:git config credential.helper '$shortname -f AUTHFILE -d'#或查看打开的文件和找到的数据:git config credential.helper '$shortname -f AUTHFILE -v'

是否有办法在使用 https://github 时跳过密码输入"

<小时>

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

参见 commit 786ef50commit f07eeed(2018 年 5 月 12 日),Luis Marsano)``).
(由 Junio C Hamano 合并 -- gitster --提交 017b7c5,2018 年 5 月 30 日)

<块引用>

git-credential-netrc:接受 gpg 选项

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

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?

解决方案

Is it possible to use a .netrc file on Windows?

Yes: You must:

  • define environment variable %HOME% (pre-Git 2.0, no longer needed with Git 2.0+)
  • put a _netrc file in %HOME%

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

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'

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

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

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


Luke mentions in the comments:

Using the latest version of msysgit on Windows 7, I did not need to set the HOME environment variable. The _netrc file alone did the trick.

This is indeed what I mentioned in "Trying to "install" github, .ssh dir not there":
git-cmd.bat included in msysgit does set the %HOME% environment variable:

@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"

However, I answered that netrc is 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/.netrc alternative to cURL".


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

For example, if your .git/config file contains:

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

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

[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 answer from Mark Longair details the credential cache mechanism which also allows you to not store your password in plain text as shown below.


With Git 1.8.3 (April 2013):

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

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

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

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

When no -f option is given, .authinfo.gpg, .netrc.gpg, .authinfo, and .netrc files in your home directory are used in this order.

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.)

# 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"


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

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

git-credential-netrc: accept gpg option

git-credential-netrc was 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 - 如何在 Windows 上使用 .netrc 文件来保存用户和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆