Enable git over ssh

Have you ever watched video where developer securely clone their git repos without entering a password and wondered how to set that up? Well, it’s quite simple and I’ll walk you through the steps.

This approach uses ssh keys to authenticate and then the sync occurs over ssh tunnels, encrypting the traffic. Setting up ssh keys is simple and you’ll no longer have to type in a username and password.

When you sync a repo with this command:

$ git clone https://github.com/ahoog42/andrewhoog.com.git

you are use HTTP over TLS and you’ll have to type in your username and password (unless the repo is public).

However, if you use this command:

$ git clone git@github.com:ahoog42/andrewhoog.com.git

and have properly configured ssh keys on your computer/server and in GitHub, you’ll security sync without have to type a username and password. This is not only fast, simple and secure but it’s great for scripting and automation.

To setup GitHub SSH keys, follow these steps.

1. Create a ssh key pair with the ssh-keygen command

If you’re on a new computer that does not have a .ssh folder in your home directory, you can create it and set the proper permissions with:

$ mkdir -p ~/.ssh && chmod 700 ~/.ssh

Next, generate your ssh keys with ssh-keygen:

$ ssh-keygen -t ed25519 -C "hiro@sophon github key"

The file ssh-keygen generates will be id_ followed by the algorithm, e.g. ~/.ssh/id_ed25519. The .pub holds your public key while the id_ed25519 holds your private key (sensitive!). If you have multiple keys you need to manage, you can use the -f flag to give the files created a specific name:

$ ssh-keygen -t ed25519 -C "docker-based deploy ubuntu-svr" -f ~/.ssh/ubuntu-svr

which will create these files in ~/.ssh:

ubuntu-svr_ed25519
ubuntu-svr_ed25519.pub

2. Copy the value of the public SSH key to the clipboard

Next, let’s copy the public key to our clipboard. On macOS, you can run the following:

$ cat ~/.ssh/id_ed25519.pub | pbcopy

There are lots of ways to copy the file content so go ahead and use whatever method you prefer.

3. Add your public key to GitHub

You now need to save add your public SSH key to your GitHub account:

  • Login to GitHub and navigate to your account settings
  • Click on the SSH and GPG link
  • Click New SSH Key to register the public SSH key with your account
    • Add a title that will help you identify the key, e.g. docker-based deploy ubuntu-svr
    • Paste the copied public key into Key textarea
  • Click “Add SSH Key”

4. Sync GitHub repo over ssh

You can now clone a repo (or other functions like push, pull, etc) using the SSH based GitHub URL to clone your repo:

$ git clone git@github.com:ahoog42/andrewhoog.com.git

and you should get see something like:

Cloning into 'andrewhoog.com'...
remote: Enumerating objects: 665, done.
remote: Counting objects: 100% (92/92), done.
remote: Compressing objects: 100% (69/69), done.
remote: Total 665 (delta 20), reused 89 (delta 17), pack-reused 573
Receiving objects: 100% (665/665), 88.89 MiB | 6.72 MiB/s, done.
Resolving deltas: 100% (328/328), done.