Now that we have a remote repository setup, the next logical step is to clone the repo to your live server so that you can push the changes that you make locally up to your live site.

Create SSH Key on Server

In order to clone the repo to your server, you must first setup an SSH key on your server. This process varies from host to host, so check with your hosting provider for the proper way to create SSH Keys on their platform.

Many will have a control panel like cPanel or Plesk. If that's the case, the key is usually created through that interface.

If your server does not have a control panel, you'll need to do it in the command line. If that's the case, you can follow the instructions in the video Generating an SSH Key to Connect with Remote Repositories to create your SSH key.

Add Server SSH Key to Git host

Once the key has been created, you'll need to add it to your Git host. I'll do this in BitBucket, but if you're working with GitHub, you can follow the steps in the video Adding Your SSH Key to Your GitHub Account to do the same thing there.

Copy your SSH Key

Regardless of how you created your key (via the command line or the web interface) you can copy your key by connecting to your server via ssh and typing "less ~/.ssh/id_rsa.pub", then copying everything from "ssh-rsa" to the end of your email address. When you have it copied, press "q" to quit the less application.

Now that you have it copied, you need to add it to your BitBucket account.

Adding your SSH Key to BitBucket

  • In the top right corner of the BitBucket page, click the "user" icon and select "Manage account"
  • Select "SSH keys" from the sidebar.
  • Click "Add key"
  • Label: My Git Site Live Server (The title is used to differentiate your computers because you can upload SSH keys from multiple computers. ie: Work Computer, Personal Laptop, Live Server etc.)
  • Key: (Paste your key here.)
  • Click "Add key"

Now, you'll see the label of your new Key in the list.

Testing Your New SSH Key

Now that you have your SSH key uploaded, it's a good idea to test it to make sure that it's working before you proceed.

The process to do so is actually quite simple.

  • Open a terminal window
  • Ssh into your server
  • Type "ssh -T git@bitbucket.org"
  • Press Enter

Now you might see a warning like the following:

The authenticity of host 'github.com (192.30.252.128)' 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)?

This is expected.

  • Type "yes"
  • Press Enter
  • If prompted for your password, enter the one you used when creating the ssh key on your server

Now you'll see a message like the following:

logged in as ModulesUnraveled.

You can use git or hg to connect to Bitbucket. Shell access is disabled.

If the username is yours, you've successfully setup your SSH key! (Don't worry about the "shell access" statement. You won't need that.)

If you receive an "access denied" message, read the Troubleshoot SSH Issues help page.

Clone Repo to Server

  • On the BitBucket page, click "Repositories" and select the repo you want to clone.
  • Copy the SSH URL at the top of the page
  • Switch to the terminal session where you are connected to the server via SSH
  • Optional: Delete the placeholder "public_html" directory by typing "rm -rf public_html" (If your server requires the root of your site be in a folder like "public_html" we need to delete that directory, and clone our repo to the same location with the same directory name. If you're on a VPS, you probably don't need to do this, but if you're on a shared host you probably do.)
  • Clone the repo by typing "git clone " and pressing Enter (In my case the command will be "git clone git@bitbucket.org:ModulesUnraveledScreencast/my-git-sebsite.git public_html")
  • If prompted, enter your password

At this point, you should see some indication that the repo is being cloned to your server.

When it's done, you can move into the repo by typing "cd public_html" (or whatever you named the repo directory) and typing "git status". If everything was successful, you should see the message:

# On branch master
nothing to commit (working directory clean)

This means that we can now navigate to our site in a web browser and see the site. Since this is a Drupal installation, I'm prompted to install Drupal and specify the database etc. The fact that we even see the installation page means our clone was successful, but I'll go ahead and install Drupal so that we can use it in later videos.

Now that I have the site installed on my local machine as well as my live server, I'm ready to make a change locally and push it to the live site.