John Jerald De Chavez

Managing Multiple SSH Accounts

I'll be showing some tips on how to manage multiple ssh accounts with ssh config.

Problem

Let say we have personal and work accounts on hosting platforms like Github or Bitbucket and etc. First thing we need to setup our ssh keys.

Creating ssh key based on your hosting platform. Kindly follow the instruction below:

At your ~/.ssh/config:

Host bitbucket.org
  HostName bitbucket.org
  User git
  AddKeysToAgent yes
  IdentityFile ~/.ssh/personal

Host bitbucket.org
  HostName bitbucket.org
  User git
  AddKeysToAgent yes
  IdentityFile ~/.ssh/work

Then whenever we make a git command from the work version control it would throw an error. The reason we are having conflict with our personal and work ssh config.

Solution

We can rename our Host on our ~/.ssh/config because this can be anything based on your prefrence

Host bitbucket.org-personal
  HostName bitbucket.org
  User git
  AddKeysToAgent yes
  IdentityFile ~/.ssh/personal

Host bitbucket.org-work
  HostName bitbucket.org
  User git
  AddKeysToAgent yes
  IdentityFile ~/.ssh/work

But when you try to git pull on your work account. It shows invalid url or you dont have authorize to pull from the repository.

Let say you already have the repository on your development machine. What we can do is to check the remote url of the repository.

git remote -v
origin  git@bitbucket.org:workspace/work-repo.git (fetch)
origin  git@bitbucket.org:workspace/work-repo.git (push)

We need to update the remote url based on your ~/.ssh/config for your work:

git remote set-url origin git@bitbucket.org-work:workspace/work-repo.git

# Check the changes
git remote -v
origin  git@bitbucket.org-work:workspace/work-repo.git (fetch)
origin  git@bitbucket.org-work:workspace/work-repo.git (push)

After updating your repository remote url, now you can execute some git commands like pull, push, and etc.

Let say you haven't clone the repo; what you can do before cloning the repository you should update the host of repository url based on your work ssh config host.

# from
# git clone git@bitbucket.org:workspace/work-repo.git

# to
git clone git@bitbucket.org-work:workspace/work-repo.git

Now you're succesfully clone your work repository based on your work ssh config. You can implement this on your personal account.

With this solution I hope this helps you to manage your ssh accounts with ease.

Managing with GitConfig

You can set up different Git configurations for your work and personal projects by defining separate gitconfig files for each environment.

For example, if you have a personal folder, you can create a personal.gitconfig file with the following content:

[user]
  name = John Doe Personal
  email = john@doe.personal.com

[core]
  sshCommand = "ssh -i ~/.ssh/personal"

Then, in your ~/.gitconfig file, use includeFile to manage the configuration based on directory:

[user]
  name = John Doe Personal
  email = john@doe.personal.com

[includeIf "gitdir:~/personal/"]
  path = ~/applications/.personal.gitconfig

[includeIf "gitdir:~/work/"]
  path = ~/work/.work.gitconfig

This way, whenever navigate to a subfolder within your personal directory, your git user.email and git user.name will automatically be set based on the .personal.gitconfig file.

You can apply the same approach to your work projects by creating a .work.gitconfig file and including it in your root gitconfig using includeIf.

Reference