Sub topic 5
Storing credentials with a credential helper
This chapter assumes you can clone, commit, and push, and that you have already created a personal access token (PAT) for an HTTPS remote. Here you will learn how Git remembers that token so you do not paste it on every push.
What a credential helper does
A credential helper is a small program that Git calls when a remote asks for a username and password. For GitHub over HTTPS, the "password" is your token. The helper can supply a saved value, or save the value Git just received after you typed it.
Git talks to helpers through a fixed set of actions: get (look up a credential), store (save one), and erase (delete one). Git calls erase when the server rejects a credential, which is why an expired token disappears from the store and you are prompted again.
Check your current helper
Run this to see which helper is configured:
git config --global credential.helperIf it prints nothing, no helper is set and Git prompts every time. Other values include cache, store, osxkeychain, and manager.
Choosing a helper
| Helper | Where the token lives | Notes |
|---|---|---|
| `cache` | In memory, for a limited time | Gone after the timeout. Default is 900 seconds. |
| `store` | Plain text in `~/.git-credentials` | Anyone who can read the file can use your token. |
| `osxkeychain` | macOS Keychain | Encrypted by the OS. |
| `manager` | Windows Credential Manager, or the OS store on other systems | Ships with Git for Windows. |
Prefer the operating system's secure store when one is available. Use store only on a machine you fully control, because it saves the token unencrypted.
Set a helper
Each command below sets one helper. Pick one.
git config --global credential.helper osxkeychain
git config --global credential.helper manager
git config --global credential.helper 'cache --timeout=3600'After setting a helper, do a git push or git pull against an HTTPS remote. Enter your GitHub username and paste the token as the password. Git stores it, and later commands run without prompting.
Common mistakes
- Typing your account password at the prompt. Authentication fails and Git may offer the same prompt again.
- Keeping an old token in the helper after it expires or is revoked. Git erases rejected credentials in most helpers, but if it does not, remove the entry from Keychain or Credential Manager yourself.
- Setting
storeon a shared machine. The token sits in~/.git-credentialsas plain text. - Expecting a helper to affect SSH remotes. Helpers apply only to HTTPS. SSH uses keys, covered later.
Exercise
You set credential.helper to cache with the default timeout. You push in the morning and again after lunch, and Git asks for your token the second time. Why, and what would you change?
Show answer
The cache keeps the token in memory for 900 seconds by default, so it expired between pushes. Either raise the timeout (for example cache --timeout=28800) or switch to a persistent, encrypted helper such as osxkeychain or manager.