GitHub - Setting Up Your Website & SSH Key

If you are anything like me, you probably started using FTP to upload and manage your website changes. The past 5 years or so, I've been using GitHub and SSH to manage my sites. I'm going to explain how to add an SSH key to your hosting server and add your site files to GitHub.

This article is already assuming you know some basic shell and git commands.

If you don't already have one, create a backup of your website on your local machine.

Do you have a GitHub account? If not, go to GitHub and set up your free account.

SSH in to your webserver and we can start the process of creating the SSH key for you to use in GitHub.

INPUT

ssh-keygen -t rsa

OUTPUT
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):

Unless you know a specific spot you want this saved, the default location will work. Just hit enter.

OUTPUT
Created directory '/home/user/.ssh'.
Enter passphrase (empty for no passphrase):

A passphrase is optional. Simply hit enter for no passphrase.

Enter same passphrase again:

Hit enter again to confirm no passphrase will be set.

OUTPUT
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
e1:ff:6a:jd:10:3a:c5:14:2a:9a:7g:d7:c7:20:d9:fe user@server.host.com
The key's randomart image is:
+--[ RSA 2048]----+
|          .      |
|        6 .      |
|       . .       |
|     + + o       |
|    o > S        |
|     . @ = .     |
|      o B.+      |
|       o.+..     |
|       .B.u..    |
+-----------------+
INPUT

cat ~/.ssh/id_rsa.pub

The output should look something like this:

OUTPUT
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAz3kpIXGjQg+b/61HZLlRRpJY3TPQDG4Jn/k4RHCFUK1OiQPI5l4EGUmniDfLS5OcMK1ZHuA/6GQREaDLz0OqrO6V4o2cV6zZZPCs4/o77+bgM5dQTHedbv3F8ePDkTczFDbchwENgnotMPTKfE7EWciziiN4uX6Xt2j8CtscmFBNBGN/v7KT5FaxQ2hV/II59qjZed5vXRdgvZsg2W6m830/8675309JeNNy/MRBvld9BLaXWtj1ZCTF49sQ/5blsWyIw5CalUYVWSHKmxZoByeFelicia9uN29iZDrmpcZiCS6UjLCu5e6gbiLz7X76PBfbt6u2GNHkA3EIjnWjjw== user@server.host.com

Copy the SSH key starting at 'ssh-rsa' through the end of the line. You'll be using it shortly. Login to your newly created GitHub account and using the top right menu click the Settings link.

From the left menu, click SSH and GPG keys. Then click the green 'New SSH Key' button on the top right.

Give your SSH key a title. To keep things organized if you have multiple keys, I would suggest using the domain name of the site you're working on for the title (e.g., example.com). Paste the code you copied previously into the Key field and click the green 'Add SSH Key' button.

If you're prompted, enter your GitHub password.

In your SSH window, cd to your main sites public directory.

INPUT

cd www/www.example.com/html/

INPUT

git clone git@github.com:user/example.com.git .

Using the dot at the end sets the contents of the repo in the current directory. If you didn't have the dot there, the git directory /example.com would be created as well.

OUTPUT
Cloning into '.'...
The authenticity of host 'github.com (190.69.123.101)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes

During the cloning process, you'll be prompted to continue. Type 'yes' and hit enter to proceed.

OUTPUT
Warning: Permanently added 'github.com,190.69.123.101' (RSA) to the list of known hosts.
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done.

Once the clone is complete, you can check to make sure the files were cloned promptly from GitHub by using the following list all command.

INPUT

ls -a

OUTPUT
.  ..  .git  .htaccess  README.md

The .git directory is required to maintain a functioning GitHub repository so you'll want to be sure not to remove it.

INPUT

git status

OUTPUT
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean