Use SSH Keys with Git

If you want to use ssh keys with git you need to do these simple steps. Once you’ve set things up, ssh keys are very easy to use and much safer than passwords.

Here are the summary steps of using ssh keys with git:

  • Generate ssh key
  • Add Key to Github Web Account
  • Tell Your SSH client to use Key

Keep reading to learn the details…

Using Keys For Git

You need to generate a key if you don’t have already. Here is an example of adding my raspberry pi as an authorized device to modify code in the repo (I write scripts on the pi and test them as one of the platforms).

If you have keys already, they will exist in ~/.ssh/ as either rsa or ed25519 versions. In my case I use ed25519, so there should be id_ed25519 and id_ed25519.pub files. If you don’t see these two files, you’ll need to create them. I always use a password to encrypt the key. This prevents someone from taking over the device where the key is stored and using it (as easily). If you don’t want a password, just don’t type on here:

Add this Key to Github

First, you need to get your public key:

Then, add it in the web interface of your https://github.com account under settings:
Git Hub Settings

Click “Add SSH Key” and paste your pub key from above, click save.

Github New SSH Key

Tell Git Client to Use this Key

By default, if you have only 1 key, it should use it. In the event you need to specify you can use the -i /path/to/key when using git/ssh, or you can just setup your ssh client config to always use the right file:

Add the following:

Test SSH Access to Git

Everyone connects as [email protected] for the user. Your key is unique though. You can test if your key is working by typing ssh -T [email protected]:

Clone a Repo Using SSH

In the basic form, you clone a repo to your device (download the source), and by default that will be https. You’ll be asked for your password each time on https. This is annoying.

If you’re using ssh, it will only ask for the password of the ssh key and you can use various keyring management to bypass that if you desire. You will need to specify the ssh URL instead though:

If you’d like to see details on github, here is a nice article that describes how to change between ssh and https url variants: Changing Remotes URL Type

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.