2024-04-14
(Probably) forcing Git to never prompt for authentication
My major use of Git is to keep copies of the public repositories of various projects from various people. Every so often, one of the people involved gets sufficiently irritated with the life of being an open source maintainer and takes their project's repository private (or their current Git 'forge' host does it for other reasons). When this happens, on my next 'git pull', Git skids to a halt with:
; git pull Username for 'https://gitlab.com':
This is not a useful authentication prompt for me. I have no special access to these repositories; if anonymous access doesn't work, there is nothing I can enter for a username and password that will improve the situation. What I want is for Git to fail with a pull error, the same way it would if the repository URL returned a 404 or the connection to the host timed out.
(Git prompts you here because sometimes people do deal with private repositories which they have special credentials for.)
As far as I know, Git unfortunately has no configuration option or
command line option that is equivalent to OpenSSH's 'batch mode'
for ssh, where it will never prompt you for password challenges and
will instead just fail. The closest you can come is setting
core.askPass
to something that generates output (such as 'echo'), in which case
Git will try to authenticate with that bogus information, fail, and
complain much more verbosely, which is not the same thing (among
other issues, it causes the Git host to see you as trying invalid
login credentials, which may have consequences).
If you're running your 'git pull' invocations from a script, as I
often am, you can have the script set 'GIT_TERMINAL_PROMPT=0
'
(and export it into the environment). According to the documentation,
this causes Git to fail rather than prompting you for anything,
including authentication. It seems somewhat dangerous to set this
generally in my environment, since I have no idea what else Git
might someday want to prompt me about (and obviously if you need
to sometimes get prompted you can't set this). Apparently this is
incomplete if you fetch Git repositories over SSH, but I don't do
that for public repositories that I track.
(I found this environment variable along with a lot of other discussion in this serverfault question and its answers.)
Some environments that run git behind the scenes, such as the historical 'go get' behavior, default to disabling git prompts. If you use such an environment it may have already handled this for you.