Pushing your local Git repository to a DreamHost server — Linux & Mac OS X


This article assumes you have already created your website (or application) on your home computer. It then walks you through how to place it under git version control and use your DreamHost server as the 'remote' (external) repository to push to.

In this way, you'll be able to fully develop your website (or application) on your home computer while using your DreamHost server as an extra place to save your code.

Using Git for web development

The instructions in this article only 'backup' your local repository to a DreamHost server. These instructions do not update your live website.

View the following article for further instructions on how to push changes on your local computer to your live website.


Step 1 — Create an SSH key pair

This step explains how to create an SSH key pair on your computer. You'll then use this to connect to your DreamHost server.

  1. On your local computer, navigate to your user's ~/.ssh directory:
    [local ~]$ cd ~/.ssh
    If the directory doesn't exist, create it:
    [local ~]$ mkdir ~/.ssh
  2. Set up SSH keys by running the following command.
    The -C option allows you to add a comment to the private key file. The key comment may be useful to help identify the key in the future.
    [local ~]$ ssh-keygen -t rsa -b 4096 -C "DreamHost Git repo"
    Generating a public/private rsa key pair.
    Enter the file in which you wish to save they key (i.e., /Users/username/.ssh/id_rsa): dreamhost-git-key
  3. Enter a name for the file when prompted, such as dreamhost-git-key.
  4. When prompted to enter a password, click Enter twice to skip.
    Two new files are created in your user's .ssh directory:
    [local ~]$ cd ~/.ssh
    [local ~]$ ls -la

Step 2 — Upload the public key to your DreamHost server

This step copies your public key into your DreamHost server's authorized_keys file.

To run the following commands, you need the name of your DreamHost server and the user/password assigned to your website. View the following articles to locate this information:

Additionally, the DreamHost website username you're running the command with must be configured as a Shell user. View the following article on instructions on how to configure this:

  1. Run the following command to copy your key to your DreamHost server.
    If you're using Linux:
    [local ~]$ ssh-copy-id -i ~/.ssh/dreamhost-git-key.pub user@server.dreamhost.com
    If you're using Mac OS X (also works with Linux):
    [local ~]$ cat ~/.ssh/dreamhost-git-key.pub | ssh user@server.dreamhost.com "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"

    You may receive the following error when running this command:

    mkdir: cannot create directory `/home/username/.ssh': File exists

    This simply means the /.ssh directory already exists on the web server. If you see this, remove the portion of the command that creates the directory and run again. For example:

    [local ~]$ cat ~/.ssh/dreamhost-git-key.pub | ssh user@server.dreamhost.com "cat >> ~/.ssh/authorized_keys"
  2. Log into your DreamHost server via SSH:
    [local ~]$ ssh user@server.dreamhost.com

    If you receive an error when attempting to log in, you may need to use ssh-agent. See the following article for more information:

  3. Confirm your key has been added to the DreamHost server by running the following (this should output your key):
    [server]$ cat ~/.ssh/authorized_keys

Step 3 — Create a bare repository on your DreamHost server

This step creates a bare repository on your DreamHost server. This will be the remote repository you push to from your local computer.

  1. Create a new directory for your new remote repository on your DreamHost server. If this code is meant to update your existing website, you should name it the same as your website ending in .git. For example, something like example.com.git.
    [server]$ cd ~
    [server]$ mkdir example.com.git
    [server]$ cd ~/example.com.git
    [server]$ git init --bare
    [server]$ git symbolic-ref HEAD refs/heads/main
  2. Run ls -la to confirm the files and directories have been created.
    [server]$ ls -la
  3. Confirm the HEAD file is set to use main by running the following command.
    [server]$ cat HEAD
    ref: refs/heads/main
    You should see main being used.

Step 4 — Configure git on your computer

  1. Run the following commands to create your identity to be used with Git.
    [local ~]$ git config --global user.name "John Doe"
    [local ~]$ git config --global user.email johndoe@example.com
  2. Run git --version on your local computer.
    [local ~]$ git --version
    If your local git version is at least 2.28, run the following init.defaultBranch command to ensure all newly created repositories use the name main as the primary branch.
    [local ~]$ git config --global init.defaultBranch main

    If for some reason you're unable to upgrade your version of git to at least 2.28, you can still manually change the primary branch name to main after you've made the first commit below. View the following article for further details:

    The commands above create a .gitconfig file under your username.

Step 5 — Create your local git repository

This steps creates a git repository for your website on your home computer. You'll then use this to push its contents to your DreamHost server.

Run the following in your project directory to initialize the repository and create your first commit:

[local ~]$ git init
[local ~]$ git add .
[local ~]$ git commit -m "First Commit"

Step 6 — Push your local repository to the remote repository

This step pushes your local repository to your DreamHost server.

  1. Add the bare repository you created on your DreamHost server as a 'remote' within your local computer's git repository. Make sure to change user and server to your actual DreamHost username and servername.

    The word dreamhost is just an alias. It can be named anything you want. But this alias name will be used when you push your content to your DreamHost server. 

    [local ~]$ git remote add dreamhost ssh://user@server.dreamhost.com/~/example.com.git
  2. Check that the new remote exists.
    [local ~]$ git remote show
  3. Push the local repository to your DreamHost server.
    [local ~]$ git push -u dreamhost main

Step 7 — Confirm your repository was correctly pushed to your DreamHost server

  1. Log into your DreamHost server via SSH.
  2. Navigate to the /example.com.git directory.
    When you run git status in the /example.com.git directory, you'll receive the following error because this directory is not a 'working tree':
    [server]$ git status
    fatal: This operation must be run in a work tree
  3. Clone the repo to a new location by running the following, which allows you to view its contents:
    [server]$ mkdir ~/cloned-example.com.git
    [server]$ cd ~/cloned-example.com.git
    [server]$ git clone ~/example.com.git ~/cloned-example.com.git
    [server]$ ls -la
    This directory contains all the files you pushed from your local machine.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?