Linux 如何在git pull命令中输入密码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14629107/
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-08-06 18:52:37  来源:igfitidea点击:

How to input password to git pull command?

linuxwindowsgitbatch-file

提问by Matt

I have written scripts for Windows and Linux to essentially set up a new users workspace with all the git repositories from our server.

我已经为 Windows 和 Linux 编写了脚本,基本上用我们服务器上的所有 git 存储库设置了一个新的用户工作区。

I would like the user to enter the password for our server once, store it in a local variable, pass that variable to each git pullcommand, then erase the password variable and exit.

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

How can I input the password when the git pullcommand requests it? Both for Windows batch file and a Linux shell script.

git pull命令请求时如何输入密码?适用于 Windows 批处理文件和 Linux shell 脚本。

Here is code from the Linux script:

这是来自 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

I've tried various things recommended on SO and elsewhere, such as piping, reading from .txt file, etc. I would prefer to not need anything more than plain old windows cmd and Linux terminal commands. And as this script is just for set up purposes, I do not need to securely store the password permanently with something like ssh agent.

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

I'm running Windows 7 and Ubuntu 12.10, but this script is meant for setting up new users, so it should ideally work on most distributions.

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

采纳答案by VonC

I would really recommend to nottry and manage that password step, and delegate that (both on Linux and Windows) to git credential helper.
See:

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

The user will enter the password only once per session.

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

回答by Jet

Synopsis:

概要:

git pull "https://<username>:<password>@github.com/<github_account>/<repository_name>.git" <branch_name>

Example:

例子:

git pull "https://admin:[email protected]/Jet/myProject.git" master

Note: This works for me on a bash script

注意:这适用于我的 bash 脚本

回答by TaehoKang

Read the remote urlfrom gitand then insert the IDand password (PW) to the urlmight work.

读取远程urlgit,然后插入ID和密码(PW)将url可能会奏效。

For example try the following:

例如尝试以下操作:

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