DEV Community


Posted on

TIP: Dealing with multiple SSH key repositories

I have different Git repositories cloned with different SSH keys. Say, one is work code and one is personal / side project.
When I finish work on one repository and start to work on the another and want to fetch the latest changes, I have to switch SSH keys.

That is sometimes annoying as I have to execute following command every time I switch from one repo to another:

ssh-add -D # delete cached key
ssh-add ~/.ssh/my-key # add key I need to use to the ssh-agent
Enter fullscreen mode Exit fullscreen mode

There are some solutions for this, like Configuring SSH
But I found it not so nice solution, although maybe they are fully automated. You have to configure hosts, clone repository with different host, etc. I prefer something else.

Shell aliases for the rescue

I created shell script aliases that will do commands listed above:

alias sshWork="ssh-add -D; ssh-add ~/.ssh/my-work-key"
alias sshPersonal="ssh-add -D; ssh-add ~/.ssh/my-personal-key"
Enter fullscreen mode Exit fullscreen mode

Now, all I need to is to execute either sshWork or sshPersonal command and it will delete previously cached key + add new identity to the authentication agent.

It's that simple.

Top comments (1)

ccoveille profile image
Christophe Colombier