如何输入密码到git pull命令?

Mat*_*att 18 linux windows git batch-file

我已经为Windows和Linux编写了脚本,基本上建立了一个新的用户工作区,其中包含来自我们服务器的所有git存储库.

我希望用户输入我们服务器的密码一次,将其存储在本地变量中,将该变量传递给每个git pull命令,然后擦除密码变量并退出.

如何在git pull命令请求时输入密码?Windows批处理文件和Linux shell脚本都有.

以下是Linux脚本中的代码:

#!/bin/bash

echo "Enter password: "
read pswd
clear #No screen peaking

#This is repeated for each repo
location=folderName
mkdir $location
cd $location
git init
git remote add origin git@<server>:$location.git
git pull origin master 
#Above prompts for password & is where I want to automatically input $pswd
Run Code Online (Sandbox Code Playgroud)

我已尝试在SO和其他地方推荐的各种东西,例如管道,从.txt文件读取等.我宁愿不需要比普通的旧Windows cmd和Linux终端命令更多的东西.由于此脚本仅用于设置目的,因此我无需使用ssh代理等安全存储密码.

我正在运行Windows 7和Ubuntu 12.10,但是这个脚本用于设置新用户,因此理想情况下它适用于大多数发行版.

小智 40

概要:

git pull "https://<username>:<password>@github.com/<github_account>/<repository_name>.git" <branch_name>
Run Code Online (Sandbox Code Playgroud)

例:

git pull "https://admin:12345@github.com/Jet/myProject.git" master
Run Code Online (Sandbox Code Playgroud)

注意:这适用于bash脚本

  • 这仅适用于问题中未指定的 http(s)。目前还没有正确答案。一个应该但不工作的例子是`echo $PASSWORD | git pull` (2认同)

Von*_*onC 4

我真的建议不要尝试管理该密码步骤,并将其(在 Linux 和 Windows 上)委托给git credential helper
看:

用户每次会话只需输入一次密码。

  • 这甚至没有回答用户的问题。IT 只是建议使用其他东西。当然,以明文形式保持密码开放听起来很糟糕,但任何开发人员都会告诉您将密码保留在源代码控制中。(除非它不是 devops 任务)因为你会忘记它。 (6认同)
  • @Cyber​​Men 不是这个 DevOps 人。我们将密码保存在密码管理应用程序中。 (3认同)