Sub topic 12
Storing credentials with a credential helper
This chapter assumes you know the scopes from earlier chapters (system, global, local) and can run git config to set and inspect values. You should also have a remote repository that uses HTTPS.
Why a credential helper exists
When you push or fetch over HTTPS, the remote server asks who you are. Without help, Git prompts for a username and a password or token every time. A credential helper is a small program that Git calls to store and retrieve those values, so you type them once.
Credential helpers apply only to HTTPS remotes. SSH remotes authenticate with keys and never use a helper.
Most hosts, including GitHub, no longer accept account passwords for Git operations. Use a personal access token in place of the password when Git prompts you.
How Git talks to a helper
The setting is credential.helper. Git runs the helper in three situations:
- get: Git asks whether a username and password are already stored for this URL.
- store: authentication succeeded, so Git asks the helper to save the values.
- erase: authentication failed, so Git asks the helper to forget them.
Choosing a helper
| Helper | Where it keeps credentials | Typical platform |
|---|---|---|
| `cache` | In memory, for a limited time | Linux, macOS |
| `store` | Plain text file `~/.git-credentials` | Any |
| `osxkeychain` | macOS Keychain | macOS |
| `manager` | Windows Credential Manager (Git Credential Manager) | Windows, also Linux and macOS |
Prefer the helper that uses your operating system's secure storage. Git for Windows installs Git Credential Manager by default. On macOS, osxkeychain ships with the Git build from Xcode tools and Homebrew.
Setting a helper
Set the helper in the global scope so it applies to all your repositories.
git config --global credential.helper osxkeychainOn Windows or any platform with Git Credential Manager installed:
git config --global credential.helper managerOn a Linux machine with no keyring, cache keeps credentials in memory. The optional timeout is in seconds. This example keeps them for one hour:
git config --global credential.helper "cache --timeout=3600"After the next successful HTTPS push or fetch, Git stores the credentials. Later operations on that host do not prompt.
The plain-text option
The store helper writes credentials unencrypted to ~/.git-credentials.
git config --global credential.helper storeAnyone who can read your home directory can read the token. Use it only on a machine you control and where no secure helper is available.
Checking and troubleshooting
Check which helper is active and where the setting comes from, using the inspection command from earlier in this tutorial:
git config --show-origin --get-all credential.helper--get-all is useful because credential.helper can be set in several scopes, and Git consults every helper it finds. A system-level helper from your installer can combine with one you add globally.
Common problems:
- Git keeps prompting. No helper is set, or the helper failed to save. Check the output above.
- Authentication fails with a saved token. The token expired or was revoked. Git calls
eraseafter a failure for most helpers, so the next attempt prompts again. To clear it manually, rungit credential rejector remove the entry in your keychain. - Wrong account used. The helper stored credentials for the host, not per repository. Git Credential Manager lets you switch accounts, and you can set
credential.useHttpPath trueto key entries by the full repository path.
Exercise
You set credential.helper store globally on a laptop, then notice your token in ~/.git-credentials. Name two actions to take, and name a safer helper for macOS.
Show answer
Delete ~/.git-credentials and revoke the token on the hosting service, then change the helper. On macOS, use git config --global credential.helper osxkeychain, which keeps credentials in the Keychain.