How to Use Multiple GitHub Accounts
I needed to use multiple GitHub accounts, such as for personal and work purposes, so I wrote down how to do it.
First, note that you cannot register the same SSH key with multiple GitHub accounts. You need to create a separate SSH key for each account.
After creating and registering the keys, configure your ${HOME}/.ssh/config as follows:
# Connection settings for your private GitHub account
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
User git
# Connection settings for your work GitHub account
Host github.com.organizationA
HostName github.com
IdentityFile ~/.ssh/id_rsa
IdentitiesOnly yes
User git
You should include IdentitiesOnly yes. Otherwise, a key different from the one specified in IdentityFile may be used.
Next, configure your ${HOME}/.gitconfig as follows:
[user]
name = [[main account name]]
email = [[main account email]]
[includeIf "gitdir:~/repos/organizationA/"]
path = ~/.gitconfig.organizationA
Also, set up ${HOME}/.gitconfig.organizationA like this:
[user]
name = [[account name for organization A]]
email = [[account email for organization A]]
[url "git@github.com.organizationA:"]
insteadOf = git@github.com:
[url "ssh://git@github.com.organizationA/"]
insteadOf = ssh://git@github.com/
[url "git@github.com.organizationA:"]
insteadOf = https://github.com/
With these two settings, repositories placed under ${HOME}/repos/organizationA will automatically load the settings for organizationA, and the username and email address will be set accordingly.
Also, since the URL is rewritten, you can simply clone as usual from github.com.
Even if the cloned organization’s repository has a private submodule that is not specified with a relative path, this rewrite feature will ensure the protocol is unified to SSH and the correct key is selected automatically.
This setup generally works, but if you are working on a project that uses devcontainer, this alone may not be sufficient.
In my case, I wrote a setting in devcontainer.json to mount the two files directly.
{
"mounts": [
"source=${localEnv:HOME}/.ssh,target=/home/dev/.ssh,type=bind,readonly",
"source=${localEnv:HOME}/.gitconfig.organizationA,target=/home/dev/.gitconfig,type=bind,readonly"
]
}
Fortunately, in my project, devcontainer.json is not managed directly in the repository.
The policy is to copy devcontainer.example.json and modify it as needed, so this worked well.
If your project manages devcontainer.json directly in the repository, you need to think a bit more.
If there are not many changes, you can write the mount settings locally as above and simply ignore the devcontainer.json in the worktree with the following command:
git update-index --skip-worktree .devcontainer/devcontainer.json
If things get more complicated, it may be best to separate OS users altogether.