Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 263
Jon Cram Avatar asked Dec 16 '11 15:12

Jon Cram


People also ask

How should I store my Git credentials?

You can use git-credential-store to store your passwords unencrypted on the disk, protected only by the permissions of the file system. You can check the credentials stored in the file ~/. git-credentials . For more information, visit git-credential-store - Helper to store credentials on disk.


1 Answers

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.

like image 183
Patryk Ściborek Avatar answered Oct 17 '22 09:10

Patryk Ściborek