How to switch git user at terminal

Sometimes, you try to push you local repo:

git push

but obtain the following error:

remote: Permission to username1/repo.git denied to username2.
fatal: unable to access 'https://github.com/username1/repo.git/':
The requested URL returned error: 403

That's because you need to change authorization info from Keychain. To do this by:

git config user.name username2
git config user.email name2@email

git config credential.username username2

After this, git will authorize by username2, and you will see the credential section in git config:

[credential]
        username = username2
Last modified: le 2018/11/04 22:47