There are situations where you need to work with a repository GIT using the HTTPs protocol and cannot permanently store user credentials in local configuration. One solution is to temporarily cache the credentials (username and password) for a limited time.
git-credential-cache
is the helper we are going to use to temporarily store the passwords in the memory.
Also see how to set the author of a Git command for a single run of the command in this quick tip, or yet How to configure username and email per project with Git.
The command git-credential-cache
git config credential.helper 'cache [ ]'
Description
This command stores credentials in memory for use for a given time by the Git program.
Stored credentials are never stored on disk and are forgotten after a configurable timeout.
The cache can be accessed through a Unix domain socket, restricted to the current user by file system permissions.
Options
Some options are available when using the command:
--timeout
Specifies the time in seconds to cache credentials. The default value is 900 which is equivalent to 15 minutes.
--socket
use the option socket specifying the path (path on disk) to contact a daemon cache daemon running, or start a new cache daemon if not already started.
The default is $XDG_CACHE_HOME/git/credential/socket
, unless ~/.git-credential-cache/
exists, if the path exists ~/.git-credential-cache/socket
is used instead.
If your home directory is on a network-mounted file system, you may need to change it to a local file system.
When using this option you must specify an absolute path.
controlling the daemon
If you want the daemon exit before the configured time, thus forgetting all cached credentials before they time out, you can use an exit command:
git credential-cache exit
Examples
The purpose of using this helper is to reduce the number of times you will need to enter username and password in a short space of time, for example, when you are making deployment adjustments on a test server.
Check out the usage example below.
git config credential.helper cache git push https://gitlab.com/palavrasagrada.git Username: Password: [after 5 minutes] git push https://gitlab.com/palavrasagrada.git [Your cached credentials will be used automatically within the time limit]
In this site update example Holy Word Bible, the developer called the helper credential.helper cache
before performing the first push.
On the first interaction with the repository, GIT asks for the username and password to be provided, the developer normally executes by typing them.
After 5 minutes of working on the source code, a new push will be executed by the developer, however, this time the cached credentials are used without having to enter username and password again.
This will happen for the helper's standard 15 minute time frame.
To increase the time limit, you can use the option --timeout
providing the seconds:
$ git config credential.helper 'cache --timeout=3600'
In the example above the time set will be 1 hour.
keep up to date
Don't forget to follow me on twitter and sign the Mazer.dev channel on Youtube to receive updates on new articles, video lessons and free courses in software development and engineering.
Pingback: Git - quick tips, commands and tutorials - MAZER.DEV