Sep 16, 2015

How to avoid providing user/password credentials, when updating multiple git repositories?

Git repositories are storage areas for source-code and other files to keep a history of the files.

To provide access to multiple people, these repositories are kept somewhere centrally accessible.

In my current case the SCM-Manager is used.

To get a repository you do

cd <TargetDirectory>git clone http://scm-manager.example.com/scm/git/<repo>

This is asking for credentials if the access is controlled.

Now, when you are working with multiple repositories, I often do:

for d in `ls -1`; do
cd $d
git fetch --all -t --prune
cd ..
done 

to update all repositories.
But this requests the credentials for every repository.

You can already supply the credentials during the cloning in the URL with
http://<user>:<password>@scm-manager.example.com....

But, when the password has some special characters you've got a problem.

Git has support to store the credentials:

First store a helper in the config of each repository:

for d in `ls -1`; do
cd $d;
git config credential.helper 'store --file=/<path>/.git_credentials';
git config credential.http://scm-manager.example.com.username  <username>;
cd ..;
done

The first call requests a password and the following one's use the cached credential.

$ for d in `ls -1`; do  cd $d; git fetch --all -t --prune; cd ..; done
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
Fetching origin
$