Save username and password in Git
To save your username and password (or Personal Access Token) in Git, you can configure a credential helper. A credential helper is a Git component that securely stores your authentication information in memory or on disk, so you don't have to enter it every time you interact with a remote repository.
Major Git hosts like GitHub and GitLab no longer accept account passwords over HTTPS. When Git prompts for a password while cloning or pushing, paste a scoped Personal Access Token (PAT)—which your OS credential helper caches identically—or use SSH keys instead.
Using Git's Built-in Credential Helpers
Git comes with several built-in credential helpers. The most common ones are cache, store, and platform-specific helpers.
1. Cache Your Credentials for a Short Time
The cache helper stores your credentials in memory for a short period of time. It's a good option for public or shared computers where you don't want to save credentials permanently.
git config --global credential.helper cache
By default, Git will remember your password for 15 minutes. You can change the timeout with the credential.helper command.
# Set the timeout to one hour (3600 seconds)
git config --global credential.helper 'cache --timeout=3600'
2. Store Your Credentials Permanently (Not Recommended)
The store helper saves your credentials to a plain-text file on disk. This is not recommended for most use cases as it is insecure. Your username and password will be saved in a file, which can be easily read by anyone with access to your system.
git config --global credential.helper store
This command will create a .git-credentials file in your home directory, which will contain your credentials.
Using a Platform-Specific Credential Manager
The most secure and recommended way to save your credentials is to use a credential manager specific to your operating system. These helpers integrate with your OS's keychain or secure storage mechanisms.
macOS
macOS has a built-in credential helper that integrates with the Keychain.
git config --global credential.helper osxkeychain
This will securely save your credentials to your macOS Keychain after the first time you enter them.
Windows
For Windows, the Git Credential Manager (GCM) is the official and recommended helper. It ships with the Git for Windows installation. GCM securely stores your credentials in the Windows Credential Manager.
# GCM is often the default, but you can explicitly set it
git config --global credential.helper manager
Linux
Linux has a variety of credential helpers, but a common choice is libsecret (which integrates with GNOME Keyring) or cache. You may need to install a package for a specific helper.
# Using the libsecret helper for GNOME Keyring
git config --global credential.helper libsecret
Removing Your Saved Credentials
To clear cached in-memory credentials early:
git credential-cache exit
To remove a saved host entry from macOS Keychain or any configured credential helper (such as Windows GCM or store):
printf "protocol=https\nhost=github.com\n" | git credential reject
# Or directly via macOS keychain helper
git credential-osxkeychain erase <<EOF
host=github.com
protocol=https
EOF
Sources
- Git Documentation: gitcredentials
- Git Documentation: git-credential-cache
- GitHub Docs: Managing Your Personal Access Tokens
