如何在命令行上将密码传递给GIT?

时间:2018-03-23 10:23:25

标签: git command-line https passwords

有很多答案,如何使用GIT凭证管理器来保存您的密码,而不是每次都输入密码。

但是我需要反方向的东西 - 我想在命令行中传递用户名/密码(不能进行用户交互),但我只想做它而不是将它存储在任何地方。这可能吗?

澄清:这将用于git clone,目前我正在使用网址语法 - https://username:password@example.com。但是,这会产生副作用,即带有密码的URL存储在origin遥控器中。这是我想避免的。

事后想法:想想看,我可以在克隆完成后简单地更改origin网址......好吧,这解决了我的问题,但我认为这个问题很好足以保持开放状态。也许它会帮助别人。

2 个答案:

答案 0 :(得分:1)

您可以使用程序expect来控制程序的所有输入和输出,包括密码(通常从终端读取而不是标准输入)。

答案 1 :(得分:1)

在命令行中传递用户名/密码。脚本:

#!/bin/sh

username="$1"
password="$2"

git push "https://$username:$password@githost.example.net/repo.git"

运行脚本传递参数:

git-script username password

另一种变体是在环境中定义用户名/密码:

username=name
password=pwd
export username password

然后脚本只是

#!/bin/sh
git push "https://$username:$password@githost.example.net/repo.git"

PS。请确保usernamepassword不包含特殊内容 字符或您需要对它们进行URL编码以在git URI中使用。