59

I'm working with Git pushing changes to a repository shared over HTTP / WebDAV, and Git prompts for a password for every operation that accesses the HTTP remote. Is there any way to make Git cache the password / have the remote server not prompt me?

The remote webserver should be an Apache and could possibly be reconfigured if necessary.

3 Answers 3

63

The way is to use ~/.netrc as outlined in step 3 of this Git documentation:

Then, add the following to your $HOME/.netrc (you can do without, but will be asked to input your password a lot of times):

machine <servername>
login <username>
password <password>

...and set permissions:

chmod 600 ~/.netrc

UPDATE:

As of git 1.7.9, it seems the way to go would be the native credential helper API. Git comes with a plaintext credential store or a less convenient but more secure temporary credential cache. It's also possible to use third-party credential helpers. So far I'm aware of a helper for the native Windows Credential Store, and one that integrates with the OS X keychain. (The Git build shipped by Homebrew has a binary for it, as might other OS X Git distributions. Github also provides a standalone binary.)

Generally, it should be sufficient to set up the a credential helper once:

git config --global credential.helper wincred

Or instead of wincred, use whichever helper is appropriate for your platform. (If the name of the helper executable is git-credential-wincred, the value you set the option to will be wincred, etc.)

The credential helpers also support the need to have separate sets of credentials for different repositories on the same host.

6
  • 7
    Also, don't include the user name in the URL for git, otherwise git won't consult ~/.netrc.
    – robinst
    Jun 17, 2010 at 22:28
  • 1
    WTF, it implies a single password for all the remote repositories? Apr 22, 2011 at 8:15
  • 1
    Git doesn't seem to implement any form of authentication internally, instead relying on the transport provider to take care of this. Neither libcurl (used for the HTTP transport), or openssh (used for the "git" transport) are aware of Git repositories, which makes per-repository authentication impossible. (Per-repository authorization, however, is possible with gitosis or most of the git hosting services.)
    – millimoose
    May 11, 2011 at 19:51
  • To answer @OlegMihailik directly: Git uses one login/password pair per remote "machine" when using HTTP, for all repositories on this machine. You could probably circumvent this when using SSH by adding one Host entry per repository into ~/.ssh/config
    – millimoose
    May 11, 2011 at 19:57
  • I am not sure what the servername in ~/.netrc is. But anyway "github.com" is working for github.com repository. Jan 10, 2022 at 5:40
37

Run this command inside your repo:

git config credential.helper store

Then push to the server once:

git push

The credentials you use to push to the server will get saved in ~/.git-credentials.

Instructions taken from this guide here.

4
  • the best and simplest way
    – nebulabox
    Mar 24, 2016 at 1:35
  • 1
    Agree with nebulabox and sidhi... I don't know why this has so few votes
    – user4805123
    Apr 17, 2016 at 16:07
  • didn't work for me. The same problem it asks again.
    – Sanan Ali
    Nov 29, 2020 at 10:57
  • @SananAli OMA! first person with the same name as mine, also same problem :(
    – user13944038
    Oct 7, 2022 at 20:29
17

Why can't you just use password in remote url?

$ git config remote.origin.url = http://username:password@origin_link.com/repository.git
1
  • 1
    This seems like a good option if you need to have a different username/password for two repositories on the same host. (A disadvantage is that at least I am more likely to make sure that ~/.netrc isn't world-readable than doing so for every .git/config)
    – millimoose
    May 21, 2012 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.