Save username and password in Git
To save your username and password 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.
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
If you need to remove your saved credentials, you can do so by running the following command:
git credential-cache exit
Or, you can simply change your credential helper to store
and then manually delete the .git-credentials
file.