Freelancer Sanity – Managing multiple Github repositories – Multiple Public/Private Keys
Why :
Ever since I forayed into the freelancing world, one thing also started happening frequently. Every client assigned me their official email address and all of the access to their different platforms was then attached to that email address.
This is good for them, but for me it meant now I have to store multiple passwords somewhere, browsers do a good job but the main problem was Git.
I use SSH to access the repositories as I don’t want to enter passwords but I cannot add the same public key to different accounts. PHPstorm helped in a way by generating tokens and making my life easy but when I wanted to do things through the terminal, there was a problem.
What did I need
I still wanted to access all these repos through the command line, without juggling the different passwords or keys
SSH config to the rescue
I had recently played around with Remote Development using VSCode and I remembered seeing the SSH config file, read a bit more about it and found a solution there!
In a nutshell the process is like this:
- Generate separate Public/Private keys for each account
- Update your
~/.ssh/config
file to create different host aliases. - Set your remote URL in your working directory to match what you setup in
~/.ssh/config
.
The steps
1. Generate separate Public/Private keys for each account
I usually use the steps mentioned here, but you just need to type one command, once each for all the different accounts.
2. Update your ~/.ssh/config
file to create different host aliases
Since both of our accounts are on Github we will use a little trick on the config file and set an alias
Host github.com-companyA HostName github.com User git IdentityFile /home/nikhil/projects/companyA/keys/id IdentitiesOnly yes Host github.com-companyB HostName github.com User git IdentityFile /home/nikhil/projects/companyB/keys/id IdentitiesOnly yes
3. Set your remote URL in your working directory to match what you setup in ~/.ssh/config
Assuming you have added your keys to your respective accounts already. Read this if you want to know how to add the public keys to your Github account. Now you can clone the different repository like following:
//Company A's Repo git clone git@github.com-companyA:cA/repo-company-A.git //Company B's Repo git clone git@github.com-companyB:cB/repo-company-B.git
And that’s it! If you have already cloned you can use git remote set-url
to update the repo URL.
I have talked about Github here as I use it more often, but this applies to any other providers like bitbucket etc, you just need to put the HostName on ~/.ssh/config
to match the host of the service. e.g
Host bitbucket.org-companyC HostName bitbucket.org User git IdentityFile /home/nikhil/projects/companyC/keys/id IdentitiesOnly yes