There are situations where you need to work with a GIT repository using HTTPS protocol and cannot permanently store user credentials in the local configuration. One solution is to store credentials in a temporary cache (username and password) for a limited time.
git-credential-cache option is what we will use to temporarily store passwords in memory.
See also how to define the author of a GIT command for a single command execution in this Set the author for a specific command, or How to configure username and email by project with GIT.
The command: git-credential-cache
git config credential.helper 'cache [<options>]'
This command stores credentials in memory for use for a certain time of the GIT program.
Credentials are never stored on disk, being forgotten after a configurable limit time.
The cache can be accessed through a Unix domain socket, restricted to the current user by file system permissions.
Some options are available to use the command:
Specifies the time in seconds to store credentials in cache. The default value is 900 equivalent to 15 minutes.
Use the socket option specifying path (disk path) to contact a cache daemon running, or start a new cache daemon if it has not been started.
The standard is
~/.git-credential-cache/ exist, if there is the way
~/.git-credential-cache/socket It is used in place.
If your personal directory is in a network-mounted file system, it may need to change to a local file system.
When using this option you must specify an absolute path.
If you wish daemon close before the setted time, thus forgetting all cache credentials before your time limit, you can use an output command:
git credential-cache exit
The goal of using this helper is to decrease the amount of times you will need to enter user and password in a short time space, for example, when making deploy adjustments to a test server.
Check out the following use example
git config credential.helper cache git push https://gitlab.com/palavrasagrada.git Username: <Your username>; Password: <your password>; # After 5 minutes git push https://gitlab.com/palavrasagrada.git # The cached credentials will be used automatically within the deadline
In this example of the site update Bíblia Palavra Sagrada, The developer called the helper
credential.helper cache before performing the first push.
In the first interaction with the repository, GIT asks the user and password to be provided, the developer normally executes them.
After 5 minutes of work in the source code, a new push will be performed by the developer, but this time the credentials stored in cache are used without having to type user and password again.
This will happen for a standard 15 minutes of Helper.
To increase the limit time it is possible to use the
--timeout option providing the seconds:
git config credential.helper 'cache --timeout=3600'
In the example above the set time will be 1 hour.