Multiple Git(Hub) accounts
At my previous job, I was able to use my personal GitHub account for accessing and committing work code, which made my GitHub setup pretty straightforward. At my new job, I need a separate work-only GitHub account. For my potential future self, as well as others in the same situation, this is how I set up my environment to handle both accounts.
Generate Keys
The first step is to generate the SSH keys for your work email. GitHub has a good guide here, so I won't repeat their work.
I named my keys personal
and work
, which come with their .pub
counterparts.
SSH Config
Create the file ~/.ssh/config
, with the following contents:
Host gh-personal
Hostname github.com
User git
IdentityFile ~/.ssh/personal
Host gh-work
Hostname github.com
User git
IdentityFile ~/.ssh/work
This will let git pick which key to use based on the host. When cloning work projects, use git clone git@gh-work:orgname/reponame.git
, while cloning personal projects can be done with git clone git@gh-personal:username/reponame.git
.
If you have already cloned repos, you can change their remotes with git remote set-url origin git@gh-personal:username/repo.git
, or with the equivalent work host.
Git Config
Create two gitconfig files, something like ~/.gitconfig-work
and ~/.gitconfig-personal
. Set your name and email in these files.
[user]
email = some@mail.com
name = Your Name
If it doesn't exist yet, create a ~/.gitconfig
file. Add the following:
[includeIf "gitdir:~/workDir/**"]
path = ~/.gitconfig-work
[includeIf "gitdir:~/projects/**"]
path = ~/.gitconfig-personal
[includeIf "gitdir:~/dotfiles/"]
path = ~/.gitconfig-personal
For my setup, I have my workDir
, which is a parent directory that contains all my work repos, so I use the **
wildcard to apply this to all repos within. Same idea with my personal projects
folder. My dotfiles
is a repo itself, which does not live within projects
, so thta one does not need the wildcard.
Troubleshooting
If you're having key or host issues, you can prepend your git operations with GIT_SSH_COMMAND="ssh -v"
to print out what is going on with the SSH connection to GitHub.