DEV Community

BC
BC

Posted on

Switch github account in terminal

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
Enter fullscreen mode Exit fullscreen mode

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:

  1. delete current one in "keychain access"
  2. run "git push" to trigger the pop-up window to input your "personal access token"

Reference

Top comments (0)