git clone - fail instead of prompting for credentials

When cloning git repositories in automated tools - web front ends, CI systems, sometimes the git clone invocation opens up a prompt asking for the username and password (for example, when cloning a non-existent Github repo or on a new node missing ssh keys).

How do I make git just fail (preferably with a sensible error message and exit code) instead of waiting for the server process to give it a username and password?


In git version 2.3 there's an environment variable GIT_TERMINAL_PROMPT which when set to 0 will disable prompting for credentials.

You can get more info about it in man git (after updating to git version 2.3) or in this blog post on github.

Examples:

  • git clone https://github.com/some/non-existing-repo will prompt for username & password
  • GIT_TERMINAL_PROMPT=0 git clone https://github.com/some/non-existing-repo will fail without prompting for username & password

If you are using ssh authentication, and on linux, then you can create an ssh command replacement to disable this.

Create a file called "sshnoprompt.sh" with:

ssh -oBatchMode=yes $@

Make this file executable with chmod +x sshnoprompt.sh

Then when starting git:

GIT_SSH="sshnoprompt.sh" git clone foo@dummyserver:not_a_repo

And it will not allow any interactive git prompts or questions - it shouldn't be able to ask the user for anything.


How to skip, ignore and/or reject git credentials prompts

The answers above only worked partly for me when using Git-for-Windows: two different applications there were vying for my attention from the automated git pull/push scripts:

  • git-credentials-manager (developed by the GfW team AFAICT; has a clean, grey Windows interface look)
  • another dialog, which' ancestry has some definitely (crappy looking) X-Windows genes.

To shut all of them up, without uninstalling the credentials manager as mentioned in some answers here: https://stackoverflow.com/questions/37182847/how-do-i-disable-git-credential-manager-for-windows, here's what now works, sitting on top of the automated (bash) shell scripts:

# https://serverfault.com/questions/544156/git-clone-fail-instead-of-prompting-for-credentials
export GIT_TERMINAL_PROMPT=0
# next env var doesn't help...
export GIT_SSH_COMMAND='ssh -oBatchMode=yes'
# these should shut up git asking, but only partly: the X-windows-like dialog doesn't pop up no more, but ...
export GIT_ASKPASS=echo
export SSH_ASKPASS=echo
# We needed to find *THIS* to shut up the bloody git-for-windows credential manager: 
# https://stackoverflow.com/questions/37182847/how-do-i-disable-git-credential-manager-for-windows#answer-45513654
export GCM_INTERACTIVE=never

What did NOT work

  • All the incantations of git config credential.modalprompt false -- mentioned as a solution in answers to that linked SO question. no dice for me.
  • fiddling with that GIT_SSH_COMMAND environment variable: nothing worked but keeping it as I suspect it'll kick in when running this stuff on a Linux box instead of Windows.
  • GIT_TERMINAL_PROMPT=0: no worky either. Kept it in for the same reason, but no dice on Windows.

Oh, and uninstalling the git credentials manager as suggested in other answers in that SO link was decided a no go option as it would definitely impact other repository trees where this thing may be useful one day. Though it was considered for a bit, before I decided against doing this.

What DID work

  • messing with those two *_ASKPASS env.vars., oddly enough shut up the 'X-Windows looking' login prompt on (automated) git push. Yay!
  • GCM_INTERACTIVE=never was the magic ingredient to finally shut up that git-for-windows credential manager dialog. Thanks to that particular answer (https://stackoverflow.com/questions/37182847/how-do-i-disable-git-credential-manager-for-windows#answer-45513654), which isn't near the top of the list, but definitely was the most important one for my case.

My git version

$ git --version
git version 2.30.1.windows.1

Parting note

<rant> Been looking for this info on-and-off for years: this time apparently I was lucky and maybe persevered longer(?), wading through the zillion pages yakking about setting up your credentials: google clearly is not intelligent. At least it does not listen very well to minority questions, that's for sure. </rant>

The title is added in hopes that google indexing places this page higher for the next one looking for answers to these questions or variations therefor...