How to cache your GitHub password in Git

Β·

2 min read

You may have come across a problem when using Git on your terminal where it asks for your username or password or sometimes even both when attempting to push or pull changes from your repository. Usually, this happens because Git tries to authenticate the owner of the repository to establish the connection. In this post, I will share how to cache Git credentials and solve this issue.

This common error occurs when trying to push an HTTPS URL containing the address of your repository. Since a connection to the remote repository is yet to be established, any attempts at trying to git fetch, pull or clone will result in Git asking for your username and password. This makes tracking changes cumbersome as you'll be required to re-enter your details for every git push or pull. In the GitHub docs page a solution provided involved using your Personal Access Token as the password when prompted by Git. I found this not quite useful as I still had to enter my password for every push.

What worked then? Well just cache your credentials in Git. This way, Git utilizes a credential helper to enable it to remember your username and password every time it connects to GitHub. Turn on your credential helper so that Git will save your password in memory for some time. The default time it stores it is 15 minutes. In your terminal enter the following:

  1. $ git config --global credential.helper cache

    It sets Git to use the credential memory cache.

  2. You can also change the default password timeout

    $ git config --global credential.helper 'cache --timeout=3600'

    This sets the cache to timeout after 1 hour (setting is in secs).

The problem should be solved and you can then push and track changes on your remote repository seamlessly. The above method works for repositories cloned using HTTPS Url. For those cloned with SSH (secure shell) protocol, you will need to provide an SSH key as a password. Prior to that you will need to have created an SSH keypair and added the public key to your Github account.

Β