You don't login to Git.
You do login to a Git repository hosting server, which requests an authentication, but Git itself has no authentication nor authorization.
(As an example of a Git repository hosting service offering login:
- GitHub:
gh auth login - GitLab:
glab auth login)
What Git does have is credential caching (check the output of git config credential helper)
On Mac: "Updating credentials from the OSX Keychain": you can check if your old user was stored there, and update it.
If you really want to disable the credential helper, you will be asked your credentials every time you push to a repository hosted on a server requesting authentication.
Regarding AgilePro's answer:
Again, there is no way to ask git to log in, instead you have to make a bogus change and then try to push the change to get it to prompt you to log in.
Not true.
Git itself does not log you in, as mentioned in my answer above.
A third-party CLI tool (like GitHub CLI gh) can connect to a repository hosting service (like github.com) and trigger the authentication.
No repository needed.
With GitHub: create first through the Web UI a Personal access tokens (classic), with minimum required scopes: repo, read:org, and gist.
Its token prefix will be ghp_....
Then authenticate yourself from the command-line, after installing gh, using gh auth login
gh auth login
You can enter your GitHub user account name, and your token.
Then, a gh auth status would give you:
C:\Users\vonc\git>gh auth status
github.com
✓ Logged in to github.com as VonC (C:\Users\vonc\AppData\Roaming\GitHub CLI\hosts.yml)
✓ Git operations for github.com configured to use https protocol.
✓ Token: ghp_************************************
✓ Token scopes: admin:org, gist, repo, user, workflow
You are authenticated (to GitHub, not to "Git").
You can also register your token with the local credential manager:
git config --global credential.helper manager
printf "protocol=https\nhost=github.com\nusername=<me>\npassword=<my_token>" |\
git-credential-manager store
This is purely for safekeeping, and does not authenticate you.
I had a similar problem with Windows. Updating Credentials Manager helped in my case.
To open Credentials Manager search that setting or navigate to:
Control Panel\All Control Panel Items\Credential Manager. In Windows Credentials -> Generic Credentials find your repo and update username/password or delete all that are not needed.
git config --global credential.helper cache
... which tells git to keep your password cached in memory for (by default) 15 minutes. You can set a longer timeout with:
git config --global credential.helper "cache --timeout=3600"
More useful links:
https://confluence.atlassian.com/bitbucketserver/permanently-authenticating-with-git-repositories-776639846.html
Using the .netrc file The .netrc file is a mechanism that allows you to specify which credentials to use for which server.
This method allows you to avoid entering a username and password every time you push to or pull from Git, but your Git password is stored in plain text.
You can store your credentials using the following command
git config credential.helper store
git push http://example.com/repo.git
# Now type username and password and it should be saved by git.
The only way I managed to push was by specifying the username:password@github.com/ in the URL in the Git push command.