Git的 - 如何使用.netrc文件在Windows上保存用户名和密码 [英] Git - How to use .netrc file on windows to save user and password
问题描述
是否可以在Windows上使用.netrc文件,当我使用Git来克隆与HTTP和用户远程仓库 - 密码
更新的 2013年4月,混帐1.8.3 :
您现在可以使用的加密的.netrc (使用GPG)。
A 新的只读凭证帮手 (在
的contrib /
)与的.netrc / .authinfo
文件已添加交互。
块引用>该脚本将允许您使用GPG加密文件NETRC ,避免其存储在一个纯文本文件的凭据的问题。
与
.gpg
扩展名的文件会被GPG解析之前进行解密。结果
多个-f
参数都OK。他们按顺序来处理,而找到的第一个匹配的条目是通过凭证帮手协议返回。
在没有
-f
选项,则.authinfo.gpg
,的.netrc .gpg
,.authinfo
,在你的主目录,的.netrc
文件使用这个命令。
块引用>要启用此凭据帮手:
混帐配置credential.helper'$短名称-f AUTHFILE1 -f AUTHFILE2
(请注意,Git会prePEND
混帐凭据 -
,以辅助名称和寻找它
在路径中。)
块引用>#如果你想大量的调试信息的:
混帐配置credential.helper'$短名称-f AUTHFILE -d#OR看发现打开的文件和数据:
混帐配置credential.helper'$短名称-f AUTHFILE -v参见一个完整的例子有没有办法使用时跳过密码输入
HTTPS: // github上
更新2012年末,随着 Git版本1.7.9 + :从这个答案 < A HREF =http://stackoverflow.com/users/223092/mark-longair>标记Longair </A>详细介绍的 凭据缓存机制 ,让您的不的明文存储密码,如下图所示。
(原来的答复)
您必须定义:
- 环境变量
%HOME%
- 把
_netrc
文件中的%HOME%
如果您使用的是Windows 7
运行CMD
键入:SETX HOME%USERPROFILE%
和%HOME%将被设置为
C:\\用户\\用户名
然后去它
并作出一个名为_netrc
注:对于Windows,你需要一个
_netrc
文件,而不是一个'的.netrc
它的内容是相当标准(更换与你的价值观):
机器&LT;主机名1&GT;
登录&LT; login1&GT;
密码&LT;&密码1 GT;
机&LT;主机名2&GT;
登录&LT; Login2身份&GT;
密码&LT;&密码2 GT;
在Windows 7上使用最新版本的msysgit,我也没必要设置HOME环境变量。在
_netrc
文件单独做的伎俩。
块引用>这的确是我在<一提到href=\"http://stackoverflow.com/questions/8514097/trying-to-install-github-ssh-dir-not-there/8531157#8531157\">Trying为
安装
github上,的.ssh
目录不存在的:结果
混帐cmd.bat
列入msysgit并设置%HOME%环境变量:@if不存在%HOME%@set HOME =%HOMEDRIVE %% HOMEPATH%
@if不存在%HOME%@set HOME =%USERPROFILE%爱国者在评论中认为,它似乎不会为HTTP协议工作
不过,我回答说,NETRC所使用的卷曲,并适用于HTTP协议,如图的这个例子(查找页面NETRC')。另外这里http协议中:<一个href=\"http://stackoverflow.com/questions/5193643/netrc-netrc-alternative-to-curl\"><$c$c>_netrc$c$c>/<$c$c>.netrc$c$c>替代
卷曲
。与在Windows NETRC支持常见的陷阱是git会绕过如果一个原点HTTPS URL指定用户名使用它。
例如,如果你的
的.git /配置
文件包含:
[遥控器原产地]
取= +裁判/头/ *:裁判/遥控器/产地/ *
URL = HTTPS://bob@$c$c.google.com/p/my-project/Git会不会通过
_netrc
解决您的凭据,来解决这个问题删除您的用户名,例如:
[遥控器原产地]
取= +裁判/头/ *:裁判/遥控器/产地/ *
URL = HTTPS://$c$c.google.com/p/my-project/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?
解决方案Update April 2013, git 1.8.3:
You now can use an encrypted .netrc (with gpg).
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
"
Update late 2012, With git version 1.7.9+: This answer from Mark Longair details the credential cache mechanism which allows you to not store your password in plain text as shown below.
(Original answer)
You must define:
- environment variable
%HOME%
- put a
_netrc
file in%HOME%
If you are using Windows 7
run the cmd type this:
setx HOME %USERPROFILE%
and the %HOME% will be set to '
C:\Users\"username"
'then go to it and make a file called '
_netrc
'Note: for Windows, you need a '
_netrc
' file, not a '.netrc
'.Its content is quite standard (Replace the 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 tocURL
".
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/
这篇关于Git的 - 如何使用.netrc文件在Windows上保存用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!