If you have 2 github accounts and want to switch in different projects to commit and push, you may need to clean some old account cache, otherwise, when you run git push
, you may see some forbidden error.
Step 0
Make sure you have the "personal access token" for both accounts. You can create such tokens by go to your github account "developer settings", then create a token there (make sure you select all "repo" related permission.
Step 1
Install git-credential-manager
:
$ brew tap microsoft/git
$ brew install --cask git-credential-manager-core
$ brew upgrade git-credential-manager-core
Step 2
Delete your current github account in "keychain access" app: Open the "keychain access" app, search "github.com", select the current item and delete it
Step 3
Back to your repo, run git push
, since git-credential-manager
auto hooked the git push
, and it doesn't find any saved password, this time it will pop up a window to ask you sign, either sign in via web or just input the "personal access token" you saved in the step 0.
Next time, if you want to switch account again, do the same process:
- delete current one in "keychain access"
- run "git push" to trigger the pop-up window to input your "personal access token"
Top comments (1)
If you're looking to switch Git profiles easily, check out GitProfiles:
GitProfiles CLI lets you:
List, add, and switch Git profiles
Update Git user.name, user.email, and SSH keys in one command
Repo: github.com/tux86/gitprofiles