13th july, 2025
The official docs on this are kind of complex. Here is what i do instead -
GitHub-CLI(gh) is the official command-line for GitHub. And we can use it to easily switch git accounts.
Run the following command to set gh
as the git credential-helper,
gh auth setup-git
credential-helpers provide username/password to git whenever we interact with GitHub(push/pull).
Login into your github accounts,
gh auth login
To switch to your desired account,
gh auth switch --user <your-username>
After switching accounts with gh auth switch
, you would be able to access respective repos.
But git commits will still use the global name and email.
This is a limitation of GitHub-CLI, it does not automatically updates name/email upon switching accounts. As a workaround, i remove my global git name and email using-
git config unset --global user.name
git config unset --global user.email
And for a repo related to account-1, i will go inside that repo and set the name and email once-
git config user.name account-1
git config user.email account-1@example.com
And repeat for other repos as per their related account.
This was a real problem for me. At some point i even had VMs for separate, headache-free, multi-account access.
GitHub-CLI is a nice offering. And i hope switching accounts also automatically configures git name and email in future.
Happy Tweaking!