Store your username and password so Git remembers them

Git needs to know who you are before it can push code to a remote repository like GitHub or GitLab. You configure this by setting a username and email address on your machine, and then choosing how Git should handle your password — either by storing it in memory for a session, saving it to disk, or using a token instead of your actual password.

The username and email you set are recorded in every commit you make. The password or token is what proves to the remote server that you have permission to push changes. These are two separate configurations, and they work differently.

Key Takeaways

  • Set your Git username and email with git config commands at either the global level (all projects) or the local level (one project only).
  • Use a personal access token instead of your actual password — most Git hosting services now require this for security reasons.
  • Store credentials using Git Credential Manager (Windows and Mac) or git credential-cache (Linux) so you do not type them repeatedly.
  • Check what you have already configured by running git config --list to see both global and local settings.
  • Local configuration overrides global configuration, so you can use different usernames for different projects if needed.

Set your username and email with git config

Open your terminal or command prompt and run these two commands in order. Replace the name and email with your own:

git config --global user.name "Your Name"

git config --global user.email "your.email@example.com"

The --global flag stores these settings in a file on your machine so every Git project you work on will use them. If you want to use a different name or email for just one project, navigate into that project's folder and run the same commands without the --global flag.

Verify what you set by running git config --list. You will see your username and email listed along with other settings. If you set both global and local versions, the local one takes priority for that project.

Use a personal access token instead of a password

GitHub, GitLab, Bitbucket, and most other hosting services no longer accept your actual account password when you push code from the command line. Instead, you generate a personal access token — 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.

On GitHub, go to Settings > Developer settings > Personal access tokens > Tokens (classic). Click "Generate new token" and give it a name like "My Laptop". Check the repo box to allow pushing and pulling code. Copy the token when ready — you will not see it again. On GitLab, the path is Settings > Access Tokens. On Bitbucket, it is Personal settings > App passwords.

When Git asks for your password, paste the token instead. Do not share this token or commit it to a repository. If you lose it or think someone saw it, delete it from your hosting service and generate a new one.

Store credentials so you do not type them every time

Typing your token every time you push gets tedious. Git can store your credentials in memory or on disk so you only enter them once per session or once per machine.

On Windows: read and install Git Credential Manager from https://github.com/git-ecosystem/git-credential-manager. It integrates with Windows and stores credentials securely. Once installed, the first time you push, Git will open a browser window to authenticate. After that, your credentials are stored.

On Mac: Git Credential Manager also works on Mac. Alternatively, macOS comes with a built-in credential helper that stores tokens in the Keychain. Run git config --global credential.helper osxkeychain to enable it.

On Linux: Run git config --global credential.helper cache to store credentials in memory for 15 minutes. If you want them stored longer, use git config --global credential.helper "cache --timeout=3600" to keep them for one hour. For permanent storage, you can use pass or secretservice, but these require additional setup.

Configure username and email per project if you need different identities

If you work on projects under different names — for example, your personal GitHub account and a work account — you can set a different username and email for each project without changing your global settings.

Navigate into the project folder and run the same git config commands without the --global flag:

git config user.name "Work Name"

git config user.email "work.email@company.com"

Now commits in this project will use the work name and email, while commits in other projects use your global settings. Run git config --list --local to see only the settings for this project, or git config --list to see both global and local together.

Troubleshoot if Git keeps asking for your password

If you set up credential storage but Git still asks for your password every time, check that the credential helper is actually configured. Run git config --list and look for a line that says credential.helper= followed by the name of your helper (cache, osxkeychain, or manager). If that line is missing, the helper is not set up.

If you see the helper listed but Git still asks, try pushing once and entering your token. The helper should cache it after the first successful push. If it does not, the helper may not be installed correctly — reinstall Git Credential Manager or run the cache command again.

If you are using SSH instead of HTTPS, you do not need to store a password at all. SSH uses a key pair instead. Check your remote URL by running git remote -v. If it starts with git@, you are using SSH and should set up an SSH key instead of a token.

Frequently Asked Questions

What is the difference between username and email in Git?

The username and email are both recorded in every commit you make and are visible to anyone who can see the repository. They identify who made the change. The email does not have to be your actual email address — it is just a field in the commit metadata. The password or token is separate and is only used to prove you have permission to push to the remote server.

Can I use the same token for multiple machines?

Yes. A personal access token works from any machine where you enter it. However, if one machine is compromised, someone could use that token to push code to your repositories. For better security, generate a different token for each machine and revoke the old one if a machine is lost or stolen.

What happens if I forget my token?

You cannot retrieve a token you have lost — Git hosting services do not show it to you after creation. Go back to your account settings, delete the old token, and generate a new one. Paste the new token the next time Git asks for your password.

Do I have to use a token, or can I use my actual password?

Most hosting services have disabled password authentication for the command line. GitHub removed it in 2021. If your service still allows it, using a token is safer because you can revoke it without changing your account password, and you can limit what it is allowed to do.

Why does Git ask for my password when I thought I set up credential storage?

The credential helper may not be installed, or the remote URL may be using SSH instead of HTTPS. Check that git config credential.helper returns a value, and check that git remote -v shows an HTTPS URL (starting with https://) rather than an SSH URL (starting with git@).