Skip to main content
  1. About
  2. Stack Internal
The 2026 Annual Developer Survey is live— take the Survey today!

Return to Answer

Post Timeline

n0099
  • reputation score 1743
  • 1 gold badge
  • 15 silver badges
  • 22 bronze badges
reformat answer
Source Link
VonC
  • reputation score 1372765
  • 570 gold badges
  • 4812 silver badges
  • 5779 bronze badges

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

UpdateYes: 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://[email protected]/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 April 2013, gitGit 1.8.3 (April 2013):

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

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

Note: withWith Git 2.1818+ (Q2June 2018), you now can customize the GPG program used to decrypt the encrypted .netrc file.

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'


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 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://[email protected]/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/

Update April 2013, git 1.8.3:

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

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

Note: with Git 2.18 (Q2 2018), you now can customize the GPG used to decrypt the encrypted .netrc file.

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'


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 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://[email protected]/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/

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://[email protected]/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 '.')

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

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

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'

add Git 2.18
Source Link
VonC
  • reputation score 1372765
  • 570 gold badges
  • 4812 silver badges
  • 5779 bronze badges

Note: with Git 2.18 (Q2 2018), you now can customize the GPG 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'


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.

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.

Note: with Git 2.18 (Q2 2018), you now can customize the GPG 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'


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.

add Windows syntax
Source Link
VonC
  • reputation score 1372765
  • 570 gold badges
  • 4812 silver badges
  • 5779 bronze badges
Loading
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot
Loading
add link
Source Link
VonC
  • reputation score 1372765
  • 570 gold badges
  • 4812 silver badges
  • 5779 bronze badges
Loading
update credential helper
Source Link
VonC
  • reputation score 1372765
  • 570 gold badges
  • 4812 silver badges
  • 5779 bronze badges
Loading
add encrypted netrc
Source Link
VonC
  • reputation score 1372765
  • 570 gold badges
  • 4812 silver badges
  • 5779 bronze badges
Loading
add link to credential helper
Source Link
VonC
  • reputation score 1372765
  • 570 gold badges
  • 4812 silver badges
  • 5779 bronze badges
Loading
added 570 characters in body
Source Link
Sam Saffron
  • reputation score 131822
  • 81 gold badges
  • 337 silver badges
  • 513 bronze badges
Loading
Vincent Scheib
  • reputation score 19036
  • 11 gold badges
  • 64 silver badges
  • 80 bronze badges
Loading
include comments
Source Link
VonC
  • reputation score 1372765
  • 570 gold badges
  • 4812 silver badges
  • 5779 bronze badges
Loading
fixed syntax error
Source Link
David Schmitt
  • reputation score 59625
  • 27 gold badges
  • 124 silver badges
  • 165 bronze badges
Loading
adding the way to set %HOME% in windows 7
Source Link
Abdulrhman Alkhodiry
  • reputation score 2268
  • 33 silver badges
  • 26 bronze badges
Loading
Source Link
VonC
  • reputation score 1372765
  • 570 gold badges
  • 4812 silver badges
  • 5779 bronze badges
Loading

AltStyle によって変換されたページ (->オリジナル) /