Monday, July 9, 2012

Git - Save username and password Credential.Helper cache

If you are using Git repository for the version controlling of your project, you must have faced this issue that each time you try to syncronize (pull/push etc. ) your local repo with the remote repo, you need to enter your username and password. And you don't find any option in Git GUI to save your credentials (username/password).

But certainly, there is a way to do that. There are two options to achieve that.

1. Credential Helpers is a secure option, released in late January 2012 along with Git version 1.7.9. The logic here is to save your credentials into memory so that you don't need to enter your username/password repeatedly.

So, with git 1.7.9 or later, you can just do the following : 
git config --global credential.helper cache
This caches the credentials for 15 minutes by default. If you want a longer timeout period, you can do the following :
 git config credential.helper 'cache --timeout=3600' 
where timeout period is defined in seconds.

2.  Another option is to set a new URL to your origin remote so that it includes you credentials.
For this, open the config file (local git repo-> .git -> config) by any text editor. There, you'll find something like : 

[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = https://xyz.com/git/example.git
So, all you need to do is modify the above url as follows : 
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = https://username:password@xyz.com/git/example.git
But some people don't prefer this second options because your git username/password will be saved in plaintext format in config file.