Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

Can I hold git credentials in environment variables?

I'd like to create a very simple shell script, which will ultimately be called by another application, that updates a local git repository:

#!/bin/bash

cd $1
sudo git pull

When executing this I'm asked for credentials (I'm pulling from a private BitBucket repository).

Can I ( briefly) store credentials in environment variables?

#!/bin/bash

export  GIT_USERNAME=<user>
export  GIT_PASSWORD=<pass>

cd $1
sudo git pull

The above doesn't work. Would anything? I could programmatically modify the origin url but that seems a bit execessive.

question from:https://stackoverflow.com/questions/8536732/can-i-hold-git-credentials-in-environment-variables

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I know that it's very old question but if you really need to pass username and password for HTTP basic authentication you can just set helper like this:

git config credential.helper '!f() { sleep 1; echo "username=${GIT_USER}"; echo "password=${GIT_PASSWORD}"; }; f'

UPDATE: I've added sleep 1 to the function. In some environments it may be probably needed due to race condition. I've got 2 virtual machines running Debian Jessie. They had the same architecture but different CPU and different number of cores. On one of these machines the helper was working fine without sleep. On the other one it wasn't. After few hours of debugging I run strace to see what's happening. And it magically started to work. strace just made git a little bit slower.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...