Multiple Git accounts VS a single Mac
Setting up multiple git SSH keys on a Mac
This is a guide for you who, like me, have suffered in the past while setting up multiple SSH keys on your MacBook, and have failed miserably and spent several hours making everything work, and when it finally worked, you didn't really get why.
Dude… that was really specific, are you ok?
Yes. Let's begin:
SSH stands for Secure Shell and it's a safety protocol, used on Shells to make secure server connections. Here, we'll not be covering how to create an SSH key,
But rather provide an easy solution to support more than one key on the same Mac at once.
The config file
When you create a new SSH Key on a Mac, it will most likely be stored at
~/.ssh/ which is a hidden folder called
.ssh at the root of your file system.
Inside this same folder, you'll need to create or modify the
$ open ~/.ssh/config
And here, we'll remove everything, and add a new Host for every account/SSH key we have. Pretend we are going to have 2 SSH keys, for now, a personal one and one from work.
The Host is the name we are going to use when cloning the project, and HostName is the actual name of the host. And the IdentityFile is the path to the SSH key that should be used on that repo.
Do not worry, it will be better explained in the next section:
After setting up the
config file, we can now clone our projects.
And here's the catch: When cloning the project, we'll use the names we defined on the Hosts, so when the terminal tries to access it, it knows which IdentityFile it will need to use.
For instance, imagine you are going to clone a project from your Personal GitHub: You go to GitHub, select to clone it using SSH, and you copy the command:
$ git clone email@example.com:mygitaccount/example.git
Notice that github.com is the Host. But since you have set on the config file a new Host to your personal account
github.com-personal, you should replace it before cloning to the same Host you defined there.
$ git clone firstname.lastname@example.org:mygitaccount/example.git
It's gonna work just fine, and when cloning, it will replace the Host with the specified HostName on the config file, clone the correct repository, and keep using the correct SSH key. Not only when cloning, but basically, with every remote operation you happen to do on this repository.
The nice thing is: The Host can be anything you’d like! I just like to add the suffix that will help me understand where it’s comming from.
More Repos! More accounts! More projects!
And eventually, more SSH keys.
You can use this approach to add as many SSH keys as you'd like to, just keep different hosts for each one, and remember to add the correct IdentityFile as well.
One more thing
Do I need to type my SSH key password EVERY TIME?
Well… if you added a password to your SSH key when you generated it, you'll need to type that password every time you are using it 😞.
UNLESS you add the following part on the config file as well:
This will allow the use of your local Keychain (encrypted storage) to store the passwords for your keys for every host. You may also add it only to specific hosts if you want.
I currently have 6 different SSH keys on my Mac, personal access, work access, and some projects I’ve been working on… And after setting it up, I never had to worry about them again.
The important part is not letting this tiny complication prevent you from working on the projects you want.
Go out there, find more projects, contribute with open source!
And keep coding.