Switch GitHub account on macOS terminal

If you have multiple GitHub accounts and are getting error messages like this while pushing code to the repo

“remote: Permission to svram/twittering.git denied to
fatal: unable to access ‘https://github.com/svram/.git/’: The requested URL returned error: 403″

This means that the currently configured username on your terminal is not the one whose repo you are intending to commit code to. The first step to fix this is to list the current configuration like so

git config --list

You will get a result which will inform you about the username and email which is currently configured.

credential.helper=osxkeychain
user.name=svram
user.email=<something>@gmail.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
core.precomposeunicode=true
remote.origin.url=https://github.com/svram/reponame.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

If your github username and email are not consistent then change those first with

git config --global user.name ""
git config --global user.email ""

One of the simple ways to do this on macOS is to delete the password of the existing GitHub account configured from the Keychain.

  • Open the Keychain, click on All Items and then search for git. You can delete the password entry which comes.
  • Once this is done, try to do a git push and it should ask you to reauthenticate. I recommend using the visual studio integration. It makes life really simple while developing code

There are other ways to do this which would work across windows, linux and macOS too. That involves generating an SSH keypair and authenticating your terminal/computer with GitHub. I recommend following this tutorial for the steps.

comments powered by Disqus