others-how to switch github accounts between repositories ?
Problem
When we use more than one account with github repositores ,sometimes ,we get this error:
➜ realrepo git:(main) git push
remote: Permission to bswen_account2/realrepo.git denied to bswen.
fatal: unable to access 'https://github.com/bswen_account2/realrepo.git/': The requested URL returned error: 403
The core error is :
fatal: unable to access 'https://github.com/bswen_account2/realrepo.git/': The requested URL returned error: 403
Environment
- MacOS 10.14
- git version 2.21.1 (Apple Git-122.3)
Reason
By default, the git would use your default github acount in local system, if you want to push to another account’s github repository, you should change your account first.
My default github account is bswen, but the new repository is owned by bswen_account2, so the error happened.
Solution
We can change the github local account by git config as follows(Assume the new account is bswen_account2):
➜ realrepo git:(main) git config credential.username bswen_account2
Now we can verify the settings as follows:
➜ realrepo git:(main) git config --local --list|grep username
credential.username=bswen_account2
Now we can use the credential to access the new github account’s repository like this:
➜ realrepo git:(main) git pull
Password for 'https://[email protected]':
➜ realrepo git:(main) git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 234 bytes | 234.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/bswen_account2/realrepo.git
5a5240f..cb130f2 main -> main
➜ realrepo git:(main)
It works!
One more thing
But what is git credencial?
Git will sometimes need credentials from the user in order to perform operations; for example, it may need to ask for a username and password in order to access a remote repository over HTTP. This manual describes the mechanisms Git uses to request these credentials, as well as some features to avoid inputting these credentials repeatedly.
You can find more information by navigating to this site.