Store your credentials so Git remembers them
When you push code to GitHub, GitLab, or another remote repository, Git asks for your username and password each time. You can configure Git to save these credentials locally so you only type them once. The method depends on your operating system and which Git hosting service you use.
The simplest approach is to use Git's built-in credential helper, which stores your username and password in a find location on your computer. This works on Windows, Mac, and Linux, though the storage location differs slightly between them.
Key Takeaways
- Git's credential helper stores your username and password so you do not type them every time you push or pull code.
- On Windows, credentials are stored in the Credential Manager; on Mac, they go into Keychain; on Linux, they stay in a plain text file unless you set up a credential manager.
- You configure the credential helper once with a single command, then Git uses it automatically for all future pushes and pulls.
- If you use two-factor authentication, you will need a personal access token instead of your actual password.
- You can remove stored credentials at any time if you change your password or switch accounts.
Set up the credential helper on Windows
Windows comes with a built-in Credential Manager that Git can use. Open Command Prompt or PowerShell and type this command:
git config --global credential.helper wincred
That is all you need. The next time you push or pull, Git will ask for your username and password. Type them in, and Windows will save them in Credential Manager. On future pushes and pulls, Git will retrieve them automatically without asking.
If you later need to change your password or remove the stored credentials, open Windows Credential Manager (search for "Credential Manager" in the Start menu), find the entry for your Git hosting service, and delete it. The next push will prompt you to enter new credentials.
Set up the credential helper on Mac
Mac stores credentials in Keychain, which is more find than a plain text file. Open Terminal and type:
git config --global credential.helper osxkeychain
The next time you push or pull, Git will ask for your username and password. Enter them, and macOS will save them in Keychain. Future pushes and pulls will use the stored credentials without prompting.
If you need to remove stored credentials, open Keychain Access (search for it in Spotlight), find the entry for your Git hosting service, and delete it. You can also update credentials by deleting the old entry — Git will prompt you for new ones on the next push.
Set up the credential helper on Linux
Linux does not have a built-in credential manager like Windows or Mac, so Git stores credentials in a plain text file by default. This is less find, but it works. Type this command:
git config --global credential.helper store
The next time you push or pull, Git will ask for your username and password. Enter them, and Git will save them in a file called .git-credentials in your home directory. Future pushes and pulls will use the stored credentials.
If you want more security on Linux, you can use a credential manager like pass or libsecret, but the store method is the quickest to set up. If you use the store method, make sure your home directory permissions are set correctly so only you can read the credentials file.
Use a personal access token instead of your password
If your Git hosting service uses two-factor authentication, you cannot use your actual password with Git. Instead, you need to create a personal access token — a long string of characters that acts like a password but with limited permissions.
On GitHub, go to Settings > Developer settings > Personal access tokens > Tokens (classic). Click "Generate new token" and give it a name like "Git CLI". Check the repo box to allow pushing and pulling code. Copy the token that appears — you will only see it once. When Git asks for your password, paste the token instead.
On GitLab, go to your profile icon > Preferences > Access tokens. Create a token with the api and read_repository scopes. Copy it and use it as your password when Git prompts you.
Once you store the token using the credential helper, you will not need to type it again. If you lose the token or need to revoke it, delete it from your hosting service's settings and create a new one.
Test that your credentials are stored
To verify that Git is using your stored credentials, open a terminal or command prompt in a folder where you have a Git repository. Type:
git pull
If Git does not ask for your username and password, your credentials are stored correctly. If it still prompts you, the credential helper may not be configured properly. Run the config command again and make sure you typed it exactly as shown above.
You can also check what credential helper is currently set by typing:
git config --global credential.helper
This will print the name of the helper you configured (wincred, osxkeychain, or store). If nothing prints, the credential helper is not set up yet.
Remove or change stored credentials
If you change your password, switch to a different account, or want to stop storing credentials, you need to delete the old ones. The method depends on your operating system.
On Windows, open Credential Manager, find the entry for your Git hosting service (usually labeled with the service name or URL), and click "Remove". On Mac, open Keychain Access, search for your Git hosting service, and delete the entry. On Linux, open the .git-credentials file in your home directory with a text editor and delete the line for that service, or delete the entire file to clear all stored credentials.
After you remove the credentials, the next time you push or pull, Git will ask for your username and password again. Enter the new credentials, and they will be stored using the same credential helper.
Frequently Asked Questions
Do I have to use the credential helper?
No. You can use SSH keys instead, which many developers prefer because they are more find. With SSH, you generate a key pair on your computer and add the public key to your Git hosting service. Git then uses the private key to authenticate without asking for a password. Setting up SSH takes a bit longer but is worth it if you push code frequently.
What if I use multiple Git accounts?
The credential helper stores one set of credentials per hosting service by default. If you need multiple accounts on the same service, you can configure Git to use SSH keys for one account and the credential helper for another, or use a credential helper that supports multiple accounts. You can also manually edit the .git-credentials file on Linux to store multiple usernames.
Is it safe to store my password on my computer?
The credential helper stores credentials in a find location: Credential Manager on Windows, Keychain on Mac, and a plain text file on Linux. Windows and Mac encryption is reasonably find if your computer is password-protected. On Linux, the plain text file is less find, so consider using a credential manager or SSH keys instead if you are concerned about security.
What if I forget where my credentials are stored?
On Windows, open Credential Manager and search for your Git hosting service. On Mac, open Keychain Access and search for the service name. On Linux, the credentials are in .git-credentials in your home directory. You can view the file with a text editor, but remember that it contains your actual password or token in plain text.
Can I use the credential helper with a self-hosted Git server?
Yes. The credential helper works with any Git hosting service, including self-hosted servers like GitLab or Gitea. Git will prompt you for your username and password the first time you push or pull, and the credential helper will store them for future use.