Force Git to fail when password required, instead of prompting for password

Viewed 334

I am working on an application where a user can enter a Git repository, which is then cloned to the local machine. In some cases (such as a mistyped URL pointing to a nonexistent repo on Github), the Git client will prompt the user for a username and password. I do not want this behavior -- I just want Git to fail with a non-zero error status. A non-interactive error message is OK, I just to avoid the password prompt causing my application to hang.

How can I achieve this?

2 Answers

You can set the GIT_TERMINAL_PROMPT environment variable to 0 if you don't want git to prompt for any input.

>>> git clone https://github.com/private/private.git 
Cloning into 'private'...
Username for 'https://github.com':
>>> GIT_TERMINAL_PROMPT=0 git clone https://github.com/private/private.git
Cloning into 'private'...
fatal: could not read Username for 'https://github.com': terminal prompts disabled

Alternatively, can set the GIT_ASKPASS environment variable or core.askpass configuration value to true (or a blank script).

>>> git clone https://github.com/private/private.git 
Cloning into 'private'...
Username for 'https://github.com':
>>> GIT_ASKPASS=true git clone https://github.com/private/private.git
Cloning into 'private'...
remote: Repository not found.
fatal: Authentication failed for 'https://github.com/private/private.git/'

Both solutions have non-zero exit codes.

In addition to GIT_TERMINAL_PROMPT=0, the following helps to also suppress credential manager windows

git -c credential.helper= <rest of commands>

Related