Monday, November 18, 2013

Set Git to not prompt for a password

Writing scripts to work with Git repositories will quickly lead you to a problem -- the interactive password prompt. The best way to make a script that doesn't ask for a password on git push or pull is to use SSH keys. However, sometimes that is not possible because an administrator has not enabled SSH key on the server for example. Another quick and dirty solution is to place the credentials into your .netrc file.

To do this, you will have to make sure that the remote URL doesn't contain the username. To see the current remotes run:

git remote

The new URL cannot have a username in it (user@server.com...). To update a URL, run:

git remote set-url origin https://server.company.com/path/to/repo.git

Now, you will have to edit the .netrc file. It is located in the user's home directory (~./netrc). Here, you will have to add an entry of the form:

machine server.company.com
login username
password password

And that's it! It should be noted that this is not a very secure method. Anybody with access to that file will have your git user's password.