25

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

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 pull command, then erase the password variable and exit.

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

Here is code from the Linux script:

#!/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.

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.

3 Answers 3

51

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

Sign up to request clarification or add additional context in comments.

5 Comments

This only works for http(s), which was not specified in the question. There's no correct answer yet. An example that should but doesn't work is echo $PASSWORD | git pull
I my usual command is git pull origin develop then how can I add pass here?
how about git fetch origin>
git fetch "https://<username>:<password>@github.com/<github_account>/<repository_name>.git" <branch_name>
Careful, this saves user/pass inside origin name, so once your pass expires, it will start failing and you'll have long time realizing why, simply because it's baked in into origin url. You'll then have to reattach the origin.
4

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

The user will enter the password only once per session.

2 Comments

This doesn't even answer the users question. IT just recommends using something else. Sure, keeping passwords open in plaintext sounds bad, but any devops person will tell you to KEEP PASSWORDS IN SOURCE CONTROL. (Unless its not a devops task) because you WILL forget it.
@CyberMen Not this DevOps person. We keep passwords in a password-keeper application.
4

Read the remote url from git and then insert the ID and password (PW) to the url might work.

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

1 Comment

Any explanation on how it works? (Because it doesn't work for me, and I'd like it did)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.