cache credentials in git

How to Cache HTTPS Credentials for Pushing Commits?

By default, Git requires users to enter their user password every time they push commit to remote repository. It can be tedious to manually type password every time for HTTP/HTTPS. For this purpose, git has introduced credential helpers from version 1.7.9. They save your credentials and automatically provide it to git every time you push commits to remote repositories. In this article, we will learn how to cache HTTPS credentials for pushing commits.

How to Cache HTTPS Credentials for Pushing Commits?

It is very easy to cache git user credentials. Open terminal and run the following command for this purpose.

$ git config --global credential.helper cache

You can use the above command to tell git to save your credentials for a specific amount of minutes. Here are some examples of the same.

# Cache for 1 hour
$ git config --global credential.helper "cache --timeout=3600"

# Cache for 1 day
$ git config --global credential.helper "cache --timeout=86400"

# Cache for 1 week
$ git config --global credential.helper "cache --timeout=604800"

After the specified duration, git will automatically clear cache contents and you will need to enter password manually, or cache the credentials again.

If you want to set the cache for infinity (forever), set the cache timeout to -1.

# Cache forever
$ git config --global credential.helper "cache --timeout=-1"

There are also third-party key stores and key rings you can use for this purpose.

# Mac OSX
$ git config --global credential.helper osxkeychain

# Windows
$ git config --global credential.helper manager

# Fedora/RHEL/CentOS
$ sudo dnf install git-credential-libsecret
$ git config --global credential.helper /usr/libexec/git-core/git-credential-libsecret

# Ubuntu/Debian
$ sudo apt-get install libsecret-1-0 libsecret-1-dev
$ cd /usr/share/doc/git/contrib/credential/libsecret
$ sudo make
$ git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret

In this article, we have learnt how to cache HTTP/HTTPS credentials in git. It is very convenient and avoids the need to manually type your password for every git push/pull command.

Also read:

How to Change Author & Committer Name of Multiple Git Commits
How to Clone Subdirectory of Git Repository
How to Get Remote URL of Local Git Repository
How to Remove File from Git Repository
How to Get One File from Another Git Branch

Leave a Reply

Your email address will not be published. Required fields are marked *