TIP: Dealing with multiple SSH key repositories

RMAG news

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

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”

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.

Leave a Reply

Your email address will not be published. Required fields are marked *