在 Bash 脚本中为 Git 提供密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31218599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:17:23  来源:igfitidea点击:

Supplying a password to Git in a Bash script

gitbash

提问by jax

From a Bash script I would like to supply a password. I have tried the following:

我想从 Bash 脚本提供密码。我尝试了以下方法:

echo 'mypass' | git pull

git pull < 'mypass'

git pull < echo 'mypass'

None seem to work.

没有一个似乎工作。

回答by barthel

gitcredentialsis the answer:

gitcredentials答案是:

DESCRIPTION

Git will sometimes need credentials from the user in order to perform operations; for example, it may need to ask for a username and password in order to access a remote repository over HTTP. This manual describes the mechanisms Git uses to request these credentials, as well as some features to avoid inputting these credentials repeatedly.

描述

Git 有时需要用户的凭据才能执行操作;例如,它可能需要要求用户名和密码才能通过 HTTP 访问远程存储库。本手册描述了 Git 用来请求这些凭据的机制,以及一些避免重复输入这些凭据的功能。

[...]

[...]

REQUESTING CREDENTIALS

Without any credential helpers defined, Git will try the following strategies to ask the user for usernames and passwords:

  1. If the GIT_ASKPASSenvironment variable is set, the program specified by the variable is invoked. A suitable prompt is provided to the program on the command line, and the user's input is read from its standard output.
  2. Otherwise, if the core.askPassconfiguration variable is set, its value is used as above.
  3. Otherwise, if the SSH_ASKPASSenvironment variable is set, its value is used as above.
  4. Otherwise, the user is prompted on the terminal.

申请证书

在没有定义任何凭证助手的情况下,Git 将尝试以下策略来询问用户的用户名和密码:

  1. 如果GIT_ASKPASS设置了环境变量,则调用该变量指定的程序。在命令行上为程序提供了一个合适的提示,用户的输入是从它的标准输出中读取的。
  2. 否则,如果core.askPass设置了配置变量,则按上述方式使用其值。
  3. 否则,如果SSH_ASKPASS设置了环境变量,则按上述方式使用其值。
  4. 否则,在终端上提示用户。

[...]

[...]

Credential helpers, on the other hand, are external programs from which Git can request both usernames and passwords; they typically interface with secure storage provided by the OS or other programs.

You may also have third-party helpers installed; search for credential-*in the output of git help -a, and consult the documentation of individual helpers. Once you have selected a helper, you can tell Git to use it by putting its name into the credential.helpervariable.

另一方面,凭证助手是外部程序,Git 可以从中请求用户名和密码;它们通常与操作系统或其他程序提供的安全存储接口。

您可能还安装了第三方助手;credential-*在 的输出中搜索git help -a,并查阅各个助手的文档。一旦你选择了一个帮助器,你可以通过将它的名称放入credential.helper变量来告诉 Git 使用它。

git help -a | grep credential-*shows the following helper:

git help -a | grep credential-*显示以下帮助程序:

  credential                remote
  credential-cache          remote-ext
  credential-cache--daemon  remote-fd
  credential-osxkeychain    remote-ftp
  credential-store          remote-ftps

回答by Seb

It is possible to include the password in the definition of your remote like so:

可以在遥控器的定义中包含密码,如下所示:

https://user:password@server

Like this you do not need to provide it for each pull.

像这样,您不需要为每次拉动都提供它。

(Much like the solution suggested in How do I provide a username and password when running “git clone [email protected]”?.)

(很像运行“git clone [email protected]”时如何提供用户名和密码中建议的解决方案。)

Warning:A better approach would be to use SSH instead of HTTPS and store your public key with GitHub. Because credentials in the remote url might land in the command history, scripts or configuration files and can then be seen by others.

警告:更好的方法是使用 SSH 而不是 HTTPS,并将您的公钥存储在 GitHub 中。因为远程 url 中的凭据可能会出现在命令历史记录、脚本或配置文件中,然后其他人可以看到。

回答by Sergey Bezugliy

  1. Create file git_password.shwith content:

    #!/bin/sh
    exec echo "$GIT_PASSWORD"
    
  2. Assign your password to the GIT_PASSWORD environment variable

    $ GIT_PASSWORD=your_password
    
  3. Execute git command with GIT_ASKPASS environment variable. It will force password prompt and execute git_password.sh as callback:

    $ GIT_ASKPASS=./git_password.sh git clone $REPO
    
  1. 创建git_password.sh包含内容的文件:

    #!/bin/sh
    exec echo "$GIT_PASSWORD"
    
  2. 将您的密码分配给 GIT_PASSWORD 环境变量

    $ GIT_PASSWORD=your_password
    
  3. 使用 GIT_ASKPASS 环境变量执行 git 命令。它将强制密码提示并执行 git_password.sh 作为回调:

    $ GIT_ASKPASS=./git_password.sh git clone $REPO
    

回答by VonC

The main techniques for feeding an input to a Bash scripts are at "Automatically enter input in command line":

向 Bash 脚本提供输入的主要技术是在命令行中自动输入输入

echo 'mypass' | git pull
# or
printf 'mypass\n' | git pull

Since Git 1.8.3, I prefer using a Git credential netrc helperwhich will fetch and feed the right password for me.

从 Git 1.8.3 开始,我更喜欢使用Git 凭据 netrc 帮助程序,它将为我获取和提供正确的密码。