How do I force Git NOT to prompt for credentials
Asked Answered
B

1

14

I'm using git clone in a Bash script that takes any type of Git location (HTTPS, Git, SSH, filesystem...) and will clone it. But this script must work without interaction with any user. More precisely, I would like this script to NOT PAUSE for credentials: if the location requires credentials, and git can't find them on its own, I want Git to fail.

My script makes the assumption that it's not its concerns if you didn't setup proper way to access to these locations without password. However, the solution should let a way to know what the failure was (outputting an error with "Authentication Failed" for example).

I need this behavior simply because it's a script, it is to be run non-interactively (cron job, VM building ... etc ...). I need it to fail and not freeze my script waiting for information that I can't feed it (and don't wan't to).

So, in my current concern, I require that git clone fails instead of asking about credentials.

I didn't find any option in git clone to force NOT to ask for credential and preferably fail on an authentication error.

Baccarat answered 9/5, 2014 at 11:6 Comment(4)
Are you using SSH to access remote repo? If so, maybe you should export your key to the server to avoid asking for credentials.Dariusdarjeeling
@GMGray I know about all the ways to setup your system or git to get access to a location without asking for credentials. This is the user's problem. My script's job is to clone repositories to which I've given various locations. I don't want it to get stuck asking for credentials as it's not meant to be an interactive script. Thanks, thou, for your remark, I'll edit my question to be clearer on these matters.Baccarat
How are you connecting? SSH or HTTP?Pole
@Pole both, I need to be compatible with any git clonable location that git is compatible with. This is a script that should not add constraints on the location (if possible). It's job will be to clone the given location, and to fail if it can't do it without interaction.Baccarat
B
18

UPDATE: in git version > 2.3:

GIT_TERMINAL_PROMPT=0 

For previous version:

Here's my trick to force failing:

GIT_ASKPASS=/bin/echo git clone ...
Baccarat answered 9/5, 2014 at 11:7 Comment(2)
this helped me diagnose an issue when using git commands in a vsts build task as the output of the prompt was not displaying even when using --progress. Thank you!Alcove
One may also need GIT_SSH_COMMAND="ssh -oBatchMode=yes" to prevent prompts when interacting over an SSH pipe.Saari

© 2022 - 2024 — McMap. All rights reserved.