Skip to content
Scott Hardy edited this page Feb 15, 2016 · 3 revisions

git-credential-env

Background

git-credential-env is a very small command-line tool to use as a Git credential helper.

From the Git website:

Credential helpers...are external programs from which Git can request both usernames and passwords; they typically interface with secure storage provided by the OS or other programs.

git-credential-env is designed to act as a Git credential helper for Git HTTP basic authentication. It sources the username and password from environment variables specified as arguments.

Git credential helpers are registered like this:

git config credential.helper some-helper-to-register

Git credential helpers usually support get, store, and erase operations. git-credential-env only supports the get operation.

Behavior

Assuming that the following environment variables are defined:

GIT_USER=foo
GIT_PASS=bar

the following command:

git-credential-env get --username=GIT_USER --password=GIT_PASS

will output:

username=foo
password=bar

The above output is what the Git credential API expects for a set of HTTP basic authentication credentials. It will attempt to authenticate using those credentials when performing actions on a Git remote.

If --username and/or --password is not specified, they will be returned as nothing:

GIT_USER=foo
git-credential-env get --username=GIT_USER

The git username will be foo and the password will be unset, i.e. git-credential-env will return:

username=foo
password=

However, if --username and/or --password is specified but undefined as an environment variable, git-credential-env will exit with status code 1 without writing anything to stdout:

GIT_USER=foo
git-credential-env get --username=SOME_UNSET_ENVIRONMENT_VAR

will output nothing, and the exit status code will be 1.

Usage

To install and register locally:

GIT_USER=foo
GIT_PASS=bar
npm install git-credential-env
git config credential.helper "$PWD/node_modules/.bin/git-credential-env --username=GIT_USER --password=GIT_PASS"

To install and register globally:

GIT_USER=foo
GIT_PASS=bar
npm install -g git-credential-env
git config credential.helper "env --username=GIT_USER --password=GIT_PASS"

Notice how in the global example, we can omit the git-credential- prefix. This is because Git automatically searches the $PATH for any available commands stating with git-credential-, and allows you to specify those helpers them without the prefix.

Alternate Method

You can accomplish the same behavior by using an inline bash function:

GIT_USER=foo
GIT_PASS=bar
git config credential.helper "!f() { echo \"username=${GIT_USER}\\npassword=${GIT_PASS}\"; }; f"

But where's the fun in that?

Clone this wiki locally