Installing Git
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 <private_file>
and <public_file>.pub
from now on.
Create config file
- Navigate to
%userprofile%\.ssh
- Create new text file and name it
config.txt
- Add these lines
Host <your_git_service>.com
HostName <your_git_service>.com
IdentityFile ~/.ssh/<private_file>
IdentitiesOnly yes
- Save it and REMOVE THE
.txt
EXTENSION Explanation: - Line 1: matches the host
<whatever>.com
ingit clone git@<whatever>.com:...
- Line 2: where to connect to, if you set it to
github.com
,git clone git@<whatever>.com:...
will be “redirected” togit clone [email protected]:...
.
Tip: set theHost
andHostName
to 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 andgit 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
yes
and hit Enter, you only need to do this once.