简体   繁体   English

通过命令行以编程方式设置 git 凭证缓存

[英]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.我正在使用命令git config credential.helper 'cache'所以我不必重复输入我的用户名和密码。 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?有没有一种方法可以初始化缓存,从可在 runitme 中使用的环境变量(例如$USER$PASS )中的值管道中,但不能长期存储它?

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.我不想使用git config user.name "your username"和等效的password命令,因为它会尝试以明文形式存储密码。 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.git-credential(1)手册页中所述,可以使用git credential approvegit credential approve缓存,但有一种更简单的方法可以执行您想要的操作。

The credential helper that you use can be an arbitrary shell script, which can produce the standard format based on the environment.您使用的凭证助手可以是任意的 shell 脚本,它可以根据环境生成标准格式。 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.当然,在很多环境中,使用无密码的 SSH 密钥更容易也更安全,但是在某些情况下,例如容器构建,不允许以安全的方式这样做,因此我上面给出的示例可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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