How to Set and Save User Credentials in Git

Azhar Bashir Khan Feb 02, 2024
How to Set and Save User Credentials in Git

This tutorial will teach us how to set and save the user credentials in Git.

Git is a version control system that tracks changes in a project directory using the Git repositories. In Git, the Git remote repositories are made secure by authentication using protocols like HTTPS.

One must provide proper credentials to access and modify the secure remote Git repositories. We can save the user credentials in Git instead of providing them whenever we need to access or modify the secure remote Git repositories.

We will now illustrate this with an example.

Set and Save User Credentials in Git

Git prompts the user to enter the user name and password whenever the user tries to access or modify the remote Git repositories using commands like git pull and git push, respectively.

We have the option to save the user credentials, viz. user name and password, in Git instead of entering those every time Git prompts for it.

We can use the Git command git config to save the user credentials. We need to execute the git config command as follows.

$ git config --global credential.helper store

The git config command above enables the storage of the user credentials on disk in Git. The credentials are stored in a file .git-credentials in the home folder, ~/.git-credentials.

Please note that the user credentials, viz. user name and password, are stored in plain text on disk.

The credentials file .git-credentials is not encrypted, and anybody who has access to the file can view the credentials.

After entering the git config command as above to enable the storage of the credentials, whenever we try to access or modify the Git remote repository, Git prompts once for the credentials, viz. user name and password.

After entering the correct credentials, those are saved on the disk by Git. Thus, we can execute the command git pull, and the Git will prompt us to enter the user name and password, which are then saved on the disk.

On subsequent calls to access or modify the remote Git repository, the saved credentials in the store on the disk are used for authentication. Thus, the user is no longer prompted to enter the user name and password.

We also have the option to use a different credential helper like a memory cache. We can use the memory cache to temporarily save the user credentials in memory.

We need to execute the git config command to use the memory cache as follows.

$ git config credential.helper 'cache --timeout=<timeout>'

The above command saves the credentials in the memory temporarily. The timeout parameter is optional and is used for the number of seconds to cache the credentials.

The above command, by default, sets the timeout to 900 seconds, viz. 15 minutes, to temporarily save the credentials in memory if the timeout parameter is not specified.

Thus, we have learned how to set and save the user credentials in Git.

For more information, please visit the links below.

  1. git-config
  2. Git Credentials

Related Article - Git Config