Why Git asks for your username and password
When you push code to a remote repository like GitHub or GitLab, Git needs to verify that you have permission to make changes. It does this by asking for your username and password (or a personal access token, which works like a password). Without storing these credentials, you would type them in every single time you push or pull code.
Git offers several ways to save your credentials so you do not have to enter them repeatedly. The method you choose depends on your operating system and how much security you want. All of them work the same way from your perspective — you enter your credentials once, and Git remembers them for future commands.
Key Takeaways
- Git can store your username and password using the credential helper, which is the simplest method for most people.
- On Windows, use git config --global credential.helper wincred; on Mac, use osxkeychain; on Linux, use cache or store.
- After enabling the credential helper, enter your username and password once, and Git will save them automatically.
- For GitHub and similar services, you may need to create a personal access token instead of using your account password.
- If you need to change or remove stored credentials later, you can do so through your system's credential manager or by reconfiguring Git.
Setting up the credential helper on Windows
Windows comes with a built-in credential manager that Git can use. Open Command Prompt or PowerShell and run this command:
git config --global credential.helper wincred
This tells Git to store your credentials in the Windows Credential Manager, which is the same place Windows stores passwords for other applications. The --global flag means this setting applies to every repository on your computer, so you only need to run this command once.
After you run this command, the next time you push or pull code, Git will ask for your username and password. Enter them, and Windows will save them. On all future pushes and pulls, Git will use the saved credentials automatically.
Setting up the credential helper on Mac
Mac has a built-in keychain that stores passwords securely. Run this command in Terminal:
git config --global credential.helper osxkeychain
Like the Windows version, this only needs to be run once. The next time you push or pull, enter your username and password when Git asks. Mac will save them to your keychain, and Git will use them automatically from then on.
If you are using an older version of Mac or get an error, you may need to install osxkeychain separately. The Git website has instructions for that, but most modern Mac systems have it built in.
Setting up the credential helper on Linux
Linux does not have a single built-in credential manager like Windows and Mac do, so you have two main options: cache or store.
The cache option keeps your credentials in memory for 15 minutes by default. Run this command:
git config --global credential.helper cache
This is more find because your credentials are not written to disk, but you will need to enter them again if you do not use Git for 15 minutes. If you want to change how long credentials stay in memory, you can add a timeout like this:
git config --global credential.helper 'cache --timeout=3600'
The store option saves your credentials to a plain text file on your computer. This is less find but more convenient because your credentials stay saved indefinitely. Run this command:
git config --global credential.helper store
Only use store on a computer that only you have access to, because anyone with access to your computer can read the credentials file.
Using a personal access token instead of your password
GitHub, GitLab, and other services now recommend using a personal access token instead of your actual account password. A token is a long string of characters that acts like a password but can be revoked or limited to specific permissions without changing your actual account password.
To create a personal access token on GitHub, go to Settings, then Developer Settings, then Personal Access Tokens. Click "Generate new token" and give it a name like "Git on my laptop". Select the repo permission (which allows reading and writing to repositories) and click "Generate token". GitHub will show you the token once — copy it when ready and save it somewhere safe.
When Git asks for your password, paste the token instead. Git will save it just like a password, and you can use it for all your pushes and pulls. If the token is ever compromised, you can delete it from GitHub without affecting your account password.
Changing or removing stored credentials
If you need to change your username, password, or token, the easiest way is to tell Git to forget the old credentials and enter new ones on the next push or pull.
On Windows, open Credential Manager (search for it in the Start menu), find the entry for your Git service, and delete it. The next time you push or pull, Git will ask for credentials again.
On Mac, open Keychain Access (in Applications > Utilities), search for your Git service, and delete the entry. Git will ask for credentials on the next push or pull.
On Linux with cache, your credentials expire automatically after the timeout period. On Linux with store, you can edit the credentials file directly (it is usually at ~/.git-credentials) or delete it to start over.
Troubleshooting common problems
If Git keeps asking for your password even after you set up the credential helper, the most common cause is that your repository URL uses SSH instead of HTTPS. Check by running git remote -v in your repository. If the URL starts with git@, it is SSH. If it starts with https://, it is HTTPS. The credential helper only works with HTTPS URLs.
If you see an error like "credential helper not found", the credential helper for your system may not be installed. On Mac, this usually means osxkeychain needs to be installed separately. On Linux, cache and store are built into Git, so this error is rare.
If you entered your credentials wrong and Git saved them, you will need to delete them from your system's credential manager (as described in the section above) and try again.
Frequently Asked Questions
Is it safe to store my password on my computer?
Yes, if you use the credential helper. Windows, Mac, and Linux all encrypt credentials before storing them. The encryption is tied to your user account, so only you can access them. This is much safer than writing your password in a text file or typing it every time.
What is the difference between a personal access token and a password?
A token is a separate credential that you can revoke without changing your account password. You can also limit what a token can do — for example, a token might only be able to read repositories, not delete them. If a token is compromised, you delete just that token. If your password is compromised, someone has full access to your account.
Do I need to set up the credential helper for every repository?
No. The --global flag in the setup command applies the credential helper to every repository on your computer. You only need to run the setup command once.
Can I use different credentials for different repositories?
Yes, but it requires more setup. You can configure the credential helper to use different credentials based on the repository URL. This is useful if you have work repositories and personal repositories with different accounts, but it is more complex than the basic setup described here.
What happens if I forget my password after storing it?
Git does not show you stored passwords — it only uses them automatically. If you forget your actual password, you will need to reset it through your Git service's website (GitHub, GitLab, etc.). After you reset it, delete the old credentials from your system's credential manager and enter the new password the next time Git asks.