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.
Top comments (1)
Your solution works but I'm afraid your solution can lead to human errors.
I would like to suggest you reading articles posted by @mbgeorge48 or @paulund
Managing multiple GitHub SSH logins on a single machine
mg ・ Apr 2
How to Manage Multiple SSH Keys for Different GitHub Accounts
Paulund ・ Apr 16