2

Git can be configured to remember passwords.

How one may check whatever given domain has stored password?

I found on https://git-scm.com/docs/git-credential one of available APIs.

For example, assuming that we are interested in GitHub credentials and username is stored on gitconfig we may run

git credential fill
protocol=https
host=github.com
<empty line>

that will return password if it is available in the credential cache. Unfortunately, it will lock (and ask the user for specifying password) if password is not available, what makes it unsuitable for a script.

https://git-scm.com/docs/gitcredentials seems to not have any info useful to solve this.

To avoid XY problem: I have a script that is supposed to automatically push commits from one of the repositories. It can wait with push until user authenticated and GitHub credential cache remembered a password.

I am strongly preferring cross-platform solutions (I will use it on Linux but I prefer to not rewrite my git setup on starting to use other OS).

reducing activity
  • 1,985
  • 2
  • 36
  • 64
  • I have edited my answer to add the proper cross-platform syntax to check if a password is registered in the credential caching of the OS. – VonC Apr 02 '21 at 07:26

1 Answers1

2

The Microsoft GCM (Git Credential Manager) includes a non-interactive mode support.

More generally (independent of the OS), a custom credential manager would always include the commands:

  • get
  • store
  • erase

A get followed by a grep should be enough to confirm if a domain has a password registered in that credential helper.

More precisely (and cross-platform solution):

printf "protocol=https\nhost=github.com"| git credential-manager-core get

Assuming your git config credential.helper returns "manager-core".
If it returns 'xxx' (anything else):

printf "protocol=https\nhost=github.com"| git credential-xxx get

Replace xxx by your value.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • `git credential get\nprotocol=https\nhost=github.com\n\n` results in `usage: git credential [fill|approve|reject]` – reducing activity Aug 12 '18 at 06:18
  • As I uderstand - you sugest to write my own credential handler so that as result I will be able to communicate with it in way suitable for what I need? – reducing activity Aug 12 '18 at 06:21
  • I clarified my question that I am preferring cross-platform support (I will use it primarily on Linix where Git-Credential-Manager-for-Windows will be of limited use). Sorry for not adding it earlier. – reducing activity Aug 12 '18 at 06:35
  • @MateuszKonieczny My answer *is* cross-platform, hence the "More generally (independent of the OS)" part. – VonC Aug 12 '18 at 08:01