简体   繁体   中英

Programmatically set git credential cache through command line

I'm using the command git config credential.helper 'cache' so I don't have to input my username and password repeatedly. However, this requires that I set the credentials one time. Is there a way that I can initialize the cache, pipe in the value from an environment variable that will be available at runitme (say $USER and $PASS ), but not have it store long term?

I don't want to use git config user.name "your username" and the equivalent password command because it will try and store the password in plaintext. I basically just want to run in a script

git config credential.helper 'cache'
git config /*set $USER in cache*/
git config /*set $PASS in cache*/

but not sure what the second and third commands would look like, if they exist.

It is possible to prime the cache by using git credential approve , as described in the git-credential(1) manual page , but there's an easier way to do what you want.

The credential helper that you use can be an arbitrary shell script, which can produce the standard format based on the environment. So you could do this:

git config credential.helper '!f() { printf "%s\n" "username=$USER" "password=$PASS"; };f'

Of course, in many environments, it is easier and more secure to use a passwordless SSH key, but some situations, such as container building, don't permit that in a secure way, so the example I gave above can be helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM