Configuring Multiple Github Accounts With Terminal
In this post let’s explore how we can setup multiple github (github.com) accounts to seamlessly perform git actions without switching between accounts and re-authenticating.
Consider you have two private github accounts hosted in github.com.
Default behavior,
In order to perform git operations (pull, push, clone, etc) on repositories hosted under one account you need authenticate using the credentials of that account, and each time you need to switch to the other account (on the same git provider) you need to authenticate again. This hinders the productivity and workflow.
Hmm… How can we overcome this and seamlessly work with repositories hosted on multiple github.com accounts?
Before we jump into the details, to get an overview of git and how git credentials are stored refer to my blog
By Default: Git Only Considers the Domain name when Storing Credentials
By default gitcredentials only considers the domain name.
So, if you have two different git accounts in two different git providers (github.com and gitlab.com) then you can seamlessly work with both accounts as gitcredentials configures two different git credential helpers for each domain with their own credentials.
For example, the .gitconfig file would have the following entries if you are using two different git accounts from two different git providers,
[credential "github.com"]
helper = osxkeychain
[credential "gitlab.com"]
helper = osxkeychain
Since there are two git credential helpers for each domain we can use two different accounts across git providers seamlessly.
But this doesn’t help much on our use-case of seamlessly working with two different git accounts on same git provider (github.com), as there will be only one git credential helper thus storing only one set of credentials.
Configure Git to consider the full Http path
By default, Git does not consider the “path” component of an http URL
This means that a credential stored for https://example.com/foo.git will also be used for https://example.com/bar.git, this is the reason we can’t use different accounts with the same git provider.
In order to overcome this, we can configure git credentials helper to consider the full path by setting the “useHttpPath” variable to true.
Execute the following commands from the terminal to setup git credential helper to consider full Http path,
Configure Git Credential Helper for “github.com” with “osxkeychain” to store the credentials,
git config --global credential.github.com.helper osxkeychain
Configure the “github.com” credential helper to use full http path,
git config --global credential.github.com.useHttpPath true
With the useHttpPath configuration git will consider each url as unique and store the credentials for each url separately in the keychain.
Using this we can set two separate credentials for https://github.com/<organizationA>/foo.git and https://github.com/<organizationB>/bar.git repositories and seamlessly perform git operations.
Note:
Changing this setting will ask your credentials for each URL (for which the credential is not stored yet). Even if you are accessing two git repositories with same git account you need to provide the credentials for each url the first time.
The .gitconfig file will have the following entries after setting up the useHttpPath,
[credential "github.com"]
useHttpPath = true
helper = osxkeychain
Hope you enjoyed the blog and able to set up multiple github (github.com) accounts to seamlessly work with Terminal.
Thank you for reading!
Cheers!!!