If you have a personal account and a work account, then it gets more tedious to use both on the same computer. Here is a step-by-step guide on how to set them up.
Step 1. Generate SSH key for each account
Refer to Github’s instructions on how to generate your SSH key and adding it to Github.
In essence, you want to generate for all your accounts and specify where to keep the keys. We name it
The corresponding public keys (
work.pub) will be created.
You will also want ssh-agent to save your passphrase:
eval "$(ssh-agent -s)" ssh-add -K ~/.ssh/personal ssh-add -K ~/.ssh/work
Step 2. SSH config
Host work HostName github.com AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/work Host personal HostName github.com AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/personal
Test that the right github user is being connected to with
ssh -T [email protected] and
ssh -T [email protected].
Step 3. Git usage
With that set up, you need to change the remotes URL. Instead of github.com, you have to replace with either work or personal.
Your personal ones look like this when you run
git remote -v:
Similarly, for work ones:
If it is not, you will want to change it with eg.
git remote set-url [email protected]:hardworker/awesome_repo.git. Of course, git clone has to use the same URL.
Step 4. Customize gitconfig
Gitconfigs can have different “levels” (system, global, local), and they will overide previous level.
Assuming the “personal” account is the default, we can set in
~/.gitconfig (the global level), and then specify an alternate gitconfig for “work”. In it, we can customize the “email” and “name” that are used in a commit.
# Personal account as default [user] email = [email protected] name = John Doe # Work account in ~/work [includeIf "gitdir:~/work/**"] path = "~/work/.gitconfig"
As you can see in the
includeIf section, we specify a path to another gitconfig. This gitconfig will only be used if the repo is in the “work” directory (or any nested subdirectories).
In the “work” gitconfig:
# Work account [user] email = [email protected] name = John Doe
You can check by running
git config github.email in different places.