Generating SSH key
- Open Terminal, type these and just hit Enter
It will generate a public key (id_ed25519.pub) and a private key (id_ed25519) in your home directory
%userprofile%\.ssh. I’m gonna call it
<public_file>.pub from now on.
Create config file
- Navigate to
- Create new text file and name it
- Add these lines
Host <your_git_service>.com HostName <your_git_service>.com IdentityFile ~/.ssh/<private_file> IdentitiesOnly yes
- Save it and REMOVE THE
- Line 1: matches the host
git clone [email protected]<whatever>.com:...
- Line 2: where to connect to, if you set it to
git clone [email protected]<whatever>.com:...will be “redirected” to
git clone [email protected]:....
Tip: set the
HostNameto the same value for convenience.
- Line 3: the private SSH key file path.
- Line 4: the SSH connection should only use the specified
IdentityFile; not any other identities which it might have access to (superuser.com)
Let’s say you have multiple GitHub accounts and multiple SSH keys. You can create multiple config files for each account and use the
IdentityFile line to specify which SSH key to use for each account.
Host github.main HostName github.com IdentityFile ~/.ssh/id_github_main IdentitiesOnly yes Host github.work HostName github.com IdentityFile ~/.ssh/id_github_work IdentitiesOnly yes
- When cloning a project, use
git clone [email protected]:...for the main account and
git clone [email protected]:...for the work account.
Add SSH key to Git service
Use the public key (id_ed25519.pub) to add to your Git service:
After that you can delete the
<public_file>.pub file if you want.
Test SSH connection
- By cloning one of your repositories using the SSH URL. It will ask you to confirm the authenticity of the host, type
yesand hit Enter, you only need to do this once.