This tutorial assumes that the reader is familiar with basic usage of git.
Git will sometimes need credentials from the user in order to perform operations; for example, it may need to ask for a username and password in order to access a remote repository over HTTP.
It can be cumbersome to input the same credentials over and over. Git provides two methods to reduce this annoyance:
- Static configuration of usernames for a given authentication context.
- Credential helpers to cache or store passwords, or to interact with a system password wallet or keychain.
Authentication context.
Git considers each credential to have a context defined by a URL. This context is used to look up context-specific configuration, and is passed to any helpers, which may use it as an index into secure storage.
For instance, imagine we are accessing https://example.com/foo.git. When Git looks into a config file to see if a section matches this context, it will consider the two a match if the context is a more-specific subset of the pattern in the config file. For example, if you have this in your config file:
[credential "https://example.com"]
username = foo
then we will match to as the context because,
protocols are exactly matched "https"
hostnames are exactly matched "example.com"
the above configuration can also be set by using command line using the following command,
git config credential.https://example.com.username foo
Note:
If hostname is not used any configuration under it will applies to all credentials
Options for a credential context
The name of an external credential helper, and any associated options.
If the helper name is not an absolute path, then the string git credential- is prepended.
for example following configuration will match to "credential-manager"
[credential]
helper = manager
You can add the same entry using the following command.
git config credential.helper manager
A default username, if one is not provided in the URL.
By default, Git does not consider the "path" component of an http URL (eg: /foo.git) to be worth matching via external helpers. This means that a credential stored for https://example.com/foo.git will also be used for https://example.com/bar.git.
To distinguish these cases, set this option to "
true".
You can add the entry using the following command.
git config credential.https://example.com.useHttpPath true
By using this option as true user can store multiple accounts/credentials for
https://example.com.
eg:
https://example.com/foo.git -> uses account foo
https://example.com/bar.git -> uses account bar
As previously mentioned Git has an internal interface for storing and retrieving credentials from system-specific helpers, as well as prompting the user for user names and passwords. The git-credential command exposes this interface to scripts which may want to retrieve, store, or prompt for credentials in the same manner as Git.
Git Credential Manager for Windows (GCM)
The
Git Credential Manager for Windows (GCM) is a credential helper for Git. It securely stores your credentials in the
Windows Credential Manager so you only need to enter them once for each remote repo you access. All future Git commands will reuse the existing credentials.
For more info on how it works refer
the official wiki.
This is a very secure method to store credentials in windows. This method is the recommended way to store credentials when using https protocol, over other alternatives such as storing the password in plain text in a config file.
2-factor authentication enable accounts can also use the GCM flow. in such cases the following procedure will be used.
- Check to see if credentials existing in the Windows Credential Manager
- If cached credentials aren’t found, the GCM will display a GitHub branded dialog asking for credential information.
- If authentication succeeds, the GCM creates a personal access token with read / write code permissions for the repo.
- The personal access token is stored in the Windows Credential Manager and used to perform the requests Git command.
Removing Credentials
To remove your stored credentials, launch
Credential Manager (Control Panel -> User Accounts and Family Safety -> Credential Manager), find your account details and delete them.