Managing multiple github accounts

When you have more than one github account, things can get messy.

  • Tags: Config, Web Development
  • Last updated: 

Note: Full credits go to https://gist.github.com/karlrado

Set up multiple gitconfigs, conditionally

This one assumes, that you have all git repositories for a company in one subdirectory. Otherwise, you can probably extend it.

1. Add the following to your ~/.gitconfig

[includeif "gitdir:~/company/*"]
        path = ~/.gitconfig_company

gitdir is the subdirectory for all repos. .gitconfig_company can be named whatever, see below

2. Create the above .gitconfig

Create ~/.gitconfig_company, then add the following code and adjust to your settings.

[core]
	sshCommand = "ssh -F ~/.ssh/config-company"

[user]
	name = <your-preferred-name>
	email = <probably-your-companies-email>

The sshCommand will use a secondary .ssh config, that we’ll need to create next.

3. Create secondary .ssh/config

Create ~/.ssh/config-company, then add the following code and adjust to your settings.

Host *
	AddKeysToAgent yes
	UseKeychain yes
	IdentityFile ~/.ssh/id_company

Host github.com
	HostName github.com
	User git
	IdentityFile ~/.ssh/id_company

You also want your standard ~/.ssh/config to look the same. This also assumes, that you have your .ssh keys setup already.

Now you can freely push to your git hostings without conflicts.