﻿{"id":983,"date":"2012-10-29T17:43:19","date_gmt":"2012-10-29T09:43:19","guid":{"rendered":"http:\/\/nick.workao.org\/?p=983"},"modified":"2012-10-29T18:22:26","modified_gmt":"2012-10-29T10:22:26","slug":"git-how-to-use-netrc-file-on-windows-to-save-user-and-password","status":"publish","type":"post","link":"http:\/\/nick.txtcc.com\/index.php\/other\/983","title":{"rendered":"Git &#8211; How to use .netrc file on windows to save user and password"},"content":{"rendered":"<p>You must define:<\/p>\n<ul>\n    <li>environment variable\u00a0<code>%HOME%<\/code><\/li>\n    <li>put a\u00a0<code>_netrc<\/code>\u00a0file in\u00a0<code>%HOME%<\/code><\/li>\n<\/ul>\n<p>If you are using Windows 7<\/p>\n<p>run the cmd type this:<\/p>\n<pre><code>setx HOME %USERPROFILE% <\/code><\/pre>\n<p>and the %HOME% will be set to &#8216;<code>C:\\Users\\&quot;username&quot;<\/code>&#8216;<\/p>\n<p>then go to it and make a file called &#8216;<code>_netrc<\/code>&#8216;<\/p>\n<p>Note: for Windows, you need a &#8216;<code>_netrc<\/code>&#8216; file, not a &#8216;<code>.netrc<\/code>&#8216;.<\/p>\n<p>Its content is quite standard (Replace the with your values):<\/p>\n<pre><code>machine &lt;hostname1&gt; \nlogin &lt;login1&gt; \npassword &lt;password1&gt; \nmachine &lt;hostname2&gt; \nlogin &lt;login2&gt; \npassword &lt;password2&gt; <\/code><\/pre>\n<hr \/>\n<p><a href=\"http:\/\/stackoverflow.com\/users\/197503\/luke\">Luke<\/a>\u00a0mentions in the comments:<\/p>\n<blockquote>Using the latest version of msysgit on Windows 7, I did not need to set the HOME environment variable. The\u00a0<code>_netrc<\/code>\u00a0file alone did the trick.<\/blockquote>\n<p>This is indeed what I mentioned in &#8220;<a href=\"http:\/\/stackoverflow.com\/questions\/8514097\/trying-to-install-github-ssh-dir-not-there\/8531157#8531157\">Trying to \u201c<code>install<\/code>\u201d github,\u00a0<code>.ssh<\/code>\u00a0dir not there<\/a>&#8220;:\n<a href=\"https:\/\/github.com\/msysgit\/msysgit\/blob\/master\/git-cmd.bat\"><code>git-cmd.bat<\/code><\/a>\u00a0included in msysgit does set the %HOME% environment variable:<\/p>\n<pre><code>@if not exist &quot;%HOME%&quot; @set HOME=%HOMEDRIVE%%HOMEPATH% @if not exist &quot;%HOME%&quot; @set HOME=%USERPROFILE% <\/code><\/pre>\n<hr \/>\n<p>\u7231\u56fd\u8005 believes in the comments that &#8220;it seems that it won&#8217;t work for http protocol&#8221;<\/p>\n<p>However, I answered that netrc is used by curl, and works for http protocol, as shown in\u00a0<a href=\"http:\/\/maymay.net\/blog\/2008\/08\/08\/how-to-use-http-basic-authentication-with-git\/\">this example<\/a>(look for &#8216;netrc&#8217; in the page): . Also used with http protocol here: &#8220;<a href=\"http:\/\/stackoverflow.com\/questions\/5193643\/netrc-netrc-alternative-to-curl\"><code>_netrc<\/code>\/<code>.netrc<\/code>\u00a0alternative to<code>cURL<\/code><\/a>&#8220;.<\/p>\n<hr \/>\n<p>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.<\/p>\n<p>For example, if your\u00a0<code>.git\/config<\/code>\u00a0file contains:<\/p>\n<pre>[remote \"origin\"]\n     fetch = +refs\/heads\/*:refs\/remotes\/origin\/*\n     url = https:\/\/bob@code.google.com\/p\/my-project\/<\/pre>\n<p>Git will not resolve your credentials via\u00a0<code>_netrc<\/code>, to fix this remove your username, like so:<\/p>\n<pre>[remote \"origin\"]\n     fetch = +refs\/heads\/*:refs\/remotes\/origin\/*\n     url = https:\/\/code.google.com\/p\/my-project\/<\/pre>","protected":false},"excerpt":{"rendered":"<p>You must define: environment variable\u00a0%HOME% put a\u00a0_netrc\u00a0file in\u00a0%HOME% If you are using Windows 7 &#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-983","post","type-post","status-publish","format-standard","hentry","category-other"],"_links":{"self":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=983"}],"version-history":[{"count":3,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/983\/revisions"}],"predecessor-version":[{"id":986,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/983\/revisions\/986"}],"wp:attachment":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=983"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}