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

时间:2013-01-31 15:19:13

标签: 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

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

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

2 个答案:

答案 0 :(得分:4)

我真的建议尝试管理密码步骤,并将其(在Linux和Windows上)委派给 git credential helper
参见:

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

答案 1 :(得分:0)

url读取远程git,然后将ID和密码(PW)插入url可能会起作用。

例如,尝试以下操作:

cd ${REPOSITORY_DIR}
origin=$(git remote get-url origin)
origin_with_pass=${origin/"//"/"//${USER_ID}:${USER_PW}@"}
git pull ${origin_with_pass} master