[git] How to change my Git username in terminal?

I was pushing and pulling from git in Terminal then I changed my username on github.com. I went to push some changes and it couldn't push because it was still recognizing my old username.. How do I change/update my username on git in terminal?

This question is related to git github git-config

The answer is


Firstly you need to change credentials from your local machine

  1. remove generic credentials if there is any

Generic credentials

  1. configure new user and email (you can make it globally if you want)
git config [--global] user.name "Your Name"
git config [--global] user.email "[email protected]"
  1. now upload or update your repo it will ask your username and password to get access to github

  1. In your terminal, navigate to the repo you want to make the changes in.
  2. Execute git config --list to check current username & email in your local repo.
  3. Change username & email as desired. Make it a global change or specific to the local repo:
    git config [--global] user.name "Full Name"
    git config [--global] user.email "[email protected]"

    Per repo basis you could also edit .git/config manually instead.
  4. Done!

When performing step 2 if you see credential.helper=manager you need to open the credential manager of your computer (Win or Mac) and update the credentials there

Here is how it look on windows enter image description here

Troubleshooting? Learn more


  1. In your terminal, navigate to the repo you want to make the changes in.
  2. Execute git config --list to check current username & email in your local repo.
  3. Change username & email as desired. Make it a global change or specific to the local repo:

git config [--global] user.name "Full Name"

git config [--global] user.email "[email protected]"

Per repo basis you could also edit .git/config manually instead.

  1. Done!

When performing step 2 if you see credential.helper=manager you need to open the credential manager of your computer (Win or Mac) and update the credentials there


There is a easy solution for that problem, the solution is removed the certificate the yours Keychain, the previous thing will cause that it asks again to the user and password.

Steps:

  1. Open keychain access

enter image description here

  1. Search the certificate gitHub.com.

  2. Remove gitHub.com certificate.

  3. Execute any operation with git in your terminal. this again ask your username and password.

For Windows Users find the key chain by following:

Control Panel >> User Account >> Credential Manager >> Windows Credential >> Generic Credential


  1. EDIT: In addition to changing your name and email You may also need to change your credentials:
  • To change locally for just one repository, enter in terminal, from within the repository

    git config credential.username "new_username"
    
  • To change globally use

    git config --global credential.username "new_username"
    

    (EDIT EXPLAINED: If you don't change also the user.email and user.name, you will be able to push your changes, but they will be registered in git under the previous user)

  1. Next time you push, you will be asked to enter your password

    Password for 'https://<new_username>@github.com':


I recommend you to do this by simply go to your .git folder, then open config file. In the file paste your user info:

[user]
    name = Your-Name
    email = Your-email

This should be it.


Please update new user repository URL

 git remote set-url origin https://[email protected]/repository.git

I tried using below commands, it's not working:

git config user.email "[email protected]"
git config user.name  "user"

OR

git config --global user.email "[email protected]"
git config --global user.name "user"

If you have created a new Github account and you want to push commits with your new account instead of your previous account then the .gitconfig must be updated, otherwise, you will push with the already owned Github account to the new account.

In order to fix this, you have to navigate to your home directory and open the .gitconfig with an editor. The editor can be vim, notepad++, or even notepad.

Once you have the .gitconfig open, just modify the "name" with your new Github account username that you want to push with.


**Check by executing this** 
git config --list
**Change user email** 
git config --global user.email "[email protected]"
**Change user name**
git config --global user.name "user"
**Change user credential name** 
git config --global credential.username "new_username"
**After this a window popup asking password.
Enter password and proceed.**

usually the user name resides under git config

git config --global user.name "first last"

although if you still see above doesn't work you could edit .gitconfig under your user directory of mac and update

[user]
        name = gitusername
        email = [email protected]

If you have cloned your repo using url that contains your username, then you should also change remote.origin.url property because otherwise it keeps asking password for the old username.

example:

remote.origin.url=https://<old_uname>@<repo_url>

should change to

remote.origin.url=https://<new_uname>@<repo_url>

From your terminal do:

git config credential.username "prefered username"

OR

git config --global user.name "Firstname Lastname"

there are 3 ways we can fix this issue

method-1 (command line)

To set your account's default identity globally run below commands

git config --global user.email "[email protected]"
git config --global user.name "Your Name"
git config --global user.password "your password"

To set the identity only in current repository , remove --global and run below commands in your Project/Repo root directory

git config user.email "[email protected]"
git config user.name "Your Name"
git config user.password "your password"

Example:

email -> organization email Id
name  -> mostly <employee Id> or <FirstName, LastName> 

**Note: ** you can check these values in your GitHub profile or Bitbucket profile

method-2 (.gitconfig)

create a .gitconfig file in your home folder if it doesn't exist. and paste the following lines in .gitconfig

[user]
    name = FirstName, LastName
    email = [email protected]
[http]
    sslVerify = false
    proxy = 
[https]
    sslverify = false
    proxy = https://corp\\<uname>:<password>@<proxyhost>:<proxy-port>
[push]
    default = simple
[credential]
    helper = cache --timeout=360000000
[core]
    autocrlf = false

Note: you can remove the proxy lines from the above , if you are not behind the proxy

Home directory to create .gitconfig file:

windows : c/users/< username or empID >

Mac or Linux : run this command to go to home directory cd ~

or simply run the following commands one after the other

git config --global --edit
git commit --amend --reset-author

method-3 (git credential pop up)

windows :

Control Panel >> User Account >> Credential Manager >> Windows Credential >> Generic Credential 
>> look for any github cert/credential and delete it.

then running any git command will prompt to enter new user name and password.

Mac :

command+space >> search for "keychain Access" and click ok >> 
search for any certificate/file with gitHub >> delete it.

then running any git command will prompt to enter new user name and password.


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to github

Does the target directory for a git clone have to match the repo name? Issue in installing php7.2-mcrypt How can I switch to another branch in git? How to draw checkbox or tick mark in GitHub Markdown table? How to add a new project to Github using VS Code git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 How to add empty spaces into MD markdown readme on GitHub? key_load_public: invalid format git - remote add origin vs remote set-url origin Cloning specific branch

Examples related to git-config

How to know the git username and email saved during configuration? How to save username and password in Git? git: fatal unable to auto-detect email address How to change my Git username in terminal? How do I commit case-sensitive only filename changes in Git? Unable to auto-detect email address git: 'credential-cache' is not a git command Is it possible to have different Git configuration for different projects? How to tell git to use the correct identity (name and email) for a given project? Is there a way to cache GitHub credentials for pushing commits?