Supporting Multiple Git/Github Accounts

Photo by Marcin Szmigiel from Pexels
Many times, especially when working as a freelance developer, you’ll need access to multiple Github repositories. Most likely, you will be using different Github accounts, so you’ll need to tell git which account to use. To do this, you’ll need to configure your ~/.ssh/config and ~/.gitconfig accordingly.
I won’t go into great detail in this post, as there already is really good documentation here on how to configure ssh for Github.
After generating the needed ssh keys, I setup my ~/.ssh/config like so:
Host personal
HostName github.com
User git
AddKeysToAgent yes
IdentitiesOnly yes
IdentityFile ~/.ssh/id_rsa_not_work
Host work
HostName github.com
User git
AddKeysToAgent yes
IdentitiesOnly yes
IdentityFile ~/.ssh/id_rsa_work
In my ~/.gitconfig, I do something like:
[includeIf "gitdir:~/repos/work/"]
path = .gitconfig-work
[includeIf "gitdir:~/repos/personal/"]
path = .gitconfig-personal
Then, in each config, I do something like this:
[user]
name = Walter Manger
email = {some-email}@{some-domain}.com
So, when I want to clone a Github repository, I can do something like:
git clone git@{work or personal}:{some organization}/{some repo}.git
Now, If I am in ~/repos/work, git will use the user settings defined in ~/.gitconfig-work.
