How to Cache https Credentials For Pushing Commits in Git?

When working with Git repositories over HTTPS, you might find yourself frequently prompted for your username and password when pushing commits. This can be bulky and interrupt your workflow. Thankfully, Git provides a way to cache your credentials so you don’t have to enter them repeatedly. In this article, we’ll explore how to cache HTTPS credentials for pushing commits in Git.

Table of Content

  • Why Cache Credentials?
  • Method 1: Git Credential Cache:
  • Method 2: Git Credential Store:
  • Method 3: Using SSH Keys:

Why Cache Credentials?

Caching credentials helps to:

  • Save Time: Eliminates the need to repeatedly enter your username and password.
  • Enhance Workflow: Streamlines the process of committing and pushing changes.
  • Reduce Frustration: Prevents frequent interruptions during your development process.

Method 1: Git Credential Cache:

The Git credential cache is a simple option that stores your credentials in memory for a short duration (default is 15 minutes).

Steps to Enable Git Credential Cache:

Step 1: Set the Credential Cache Timeout:

To cache credentials for a specific duration, use the following command. Replace 3600 with the number of seconds you want to cache the credentials (e.g., 3600 seconds for one hour).

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

Step 2: Push Your Changes:

When you push your changes, Git will prompt you for your username and password. These credentials will be cached for the duration specified.

git push origin master

Step 3: Verify the Credential Cache:

You can verify if your credentials are being cached by pushing another commit within the timeout period. If you are not prompted for your credentials again, it means the cache is working.

Method 2: Git Credential Store:

The Git credential store saves your credentials in a plaintext file on disk. This method is more persistent than the credential cache but has security implications as the credentials are stored unencrypted.

Steps to Enable Git Credential Store:

Step 1: Set Up the Credential Store:

Use the following command to enable the credential store:

git config --global credential.helper store

Step 2: Push Your Changes:

When you push your changes, Git will prompt you for your username and password. These credentials will be saved in a plaintext file in your home directory (~/.git-credentials).

git push origin master

Step 3: Verify the Credential Store:

Push another commit to ensure you are not prompted for credentials again.

Method 3: Using SSH Keys:

While not strictly an HTTPS credential caching method, using SSH keys can be a more secure and convenient alternative to managing credentials. SSH keys eliminate the need for passwords entirely.

Steps to Set Up SSH Keys:

Step 1: Generate SSH Keys:

If you don’t already have SSH keys, generate them using the following command. Follow the prompts to create a key pair.

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Step 2: Add Your SSH Key to the SSH Agent:

Start the SSH agent and add your SSH private key.

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

Step 3: Add Your SSH Key to GitHub/GitLab:

Copy your SSH public key to your clipboard.

cat ~/.ssh/id_rsa.pub

Go to your GitHub or GitLab account settings and add the SSH key.

Step 4: Change the Remote URL to SSH:

Update your Git remote URL to use SSH instead of HTTPS.

git remote set-url origin git@github.com:username/repo.git

Step 5: Push Your Changes:

You should now be able to push your changes without being prompted for a username and password.

git push origin master

Contact Us