Search code examples
gitcross-platform

How one may check whether credential manager in Git has password stored for a given domain?


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).


Solution

  • 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.