是否可以使用.netrc
Windows 上的文件?
是的:您必须:
- 定义环境变量
%HOME%
(Git 2.0 之前的版本,Git 2.0+ 不再需要)
- put a
_netrc
文件输入%HOME%
如果您使用的是 Windows 7/10,CMD
会话,类型:
setx HOME %USERPROFILE%
and the %HOME%
将被设置为 'C:\Users\"username"
'.
转到那个文件夹(cd %HOME%
)并创建一个名为 '_netrc
'
注意:同样,对于 Windows,您需要一个 '_netrc
' file, not a '.netrc
' file.
其内容相当标准(替换<examples>
与你的价值观):
machine <hostname1>
login <login1>
password <password1>
machine <hostname2>
login <login2>
password <password2>
Luke https://stackoverflow.com/users/197503/luke评论中提到:
在Windows 7上使用最新版本的msysgit,我不需要设置HOME
环境变量。这_netrc
仅文件就达到了目的。
这确实是我在《尝试去 ”install” github,.ssh目录不存在 https://stackoverflow.com/questions/8514097/trying-to-install-github-ssh-dir-not-there/8531157#8531157":
git-cmd.bat https://github.com/msysgit/msysgit/blob/master/git-cmd.bat包含在 msysgit 中确实设置了%HOME%
环境变量:
@if not exist "%HOME%" @set HOME=%HOMEDRIVE%%HOMEPATH%
@if not exist "%HOME%" @set HOME=%USERPROFILE%
爱国者相信评论中“好像对http协议不起作用”
然而我回答说netrc
被使用curl
,适用于HTTP协议,如图这个例子 http://maymay.net/blog/2008/08/08/how-to-use-http-basic-authentication-with-git/(寻找 'netrc
' 在页面中): .这里也与 HTTP 协议一起使用:“_netrc/.netrc替代cURL https://stackoverflow.com/questions/5193643/netrc-netrc-alternative-to-curl".
常见的陷阱 withnetrc
Windows 上的支持是,如果原始 https url 指定了用户名,则 git 将绕过使用它。
例如,如果您的.git/config
文件包含:
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = https://[email protected] /cdn-cgi/l/email-protection/p/my-project/
Git 不会通过以下方式解析您的凭据_netrc
,要解决此问题,请删除您的用户名,如下所示:
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = https://code.google.com/p/my-project/
替代解决方案:使用git 版本 1.7.9+(2012 年 1 月):这个答案 https://stackoverflow.com/a/5343146/6309 from 马克·朗吉尔 https://stackoverflow.com/users/223092/mark-longair详细介绍了凭证缓存机制 http://git-scm.com/docs/git-credential-cache which also允许您not以纯文本形式存储您的密码,如下所示。
With git 1.8.3 https://github.com/git/git/blob/master/Documentation/RelNotes/1.8.3.txt#L119-L120(2013年4月):
您现在可以使用加密的.netrc (with gpg
).
在 Windows 上:%HOME%/_netrc
(_
, not '.
')
A 新的只读凭据助手 https://github.com/git/git/blob/master/contrib/credential/netrc/git-credential-netrc (in contrib/
)与交互.netrc/.authinfo
文件已添加。
该脚本将允许你使用 gpg 加密的 netrc 文件,避免将凭据存储在纯文本文件中的问题。
文件与.gpg
扩展名在解析之前会被 GPG 解密。
多种的-f
论据没问题。它们按顺序进行处理,并通过凭证辅助协议返回找到的第一个匹配条目。
When no -f
给出了选项,.authinfo.gpg
, .netrc.gpg
, .authinfo
, and .netrc
您的主目录中的文件按此顺序使用。
要启用此凭证助手:
git config credential.helper '$shortname -f AUTHFILE1 -f AUTHFILE2'
(请注意,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'
完整示例请参见“有没有办法在使用时跳过密码输入https:// github https://stackoverflow.com/a/18362082/6309"
使用 Git 2.18+(2018 年 6 月),您现在可以自定义用于解密加密的 GPG 程序.netrc
file.
See commit 786ef50 https://github.com/git/git/commit/786ef50a23cbd0e93d1e41982cfaba76801ed885, commit f07eeed https://github.com/git/git/commit/f07eeed123b8880b1723b1ea9d6d6f41cfb34532 (12 May 2018) by Luis Marsano (``) https://github.com/.
(Merged by Junio C Hamano -- gitster -- https://github.com/gitster in commit 017b7c5 https://github.com/git/git/commit/017b7c52fc00897c72f6c4808ded43c4c5f9c5b8, 30 May 2018)
git-credential-netrc
: 接受gpg
option
git-credential-netrc
被硬编码以解密 'gpg
' 不管
gpg.program 选项。
这是像 Debian 这样的发行版上的一个问题,这些发行版将现代 GnuPG 称为其他名称,例如“gpg2
'