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

Overview

These instructions assume that you've created your application or website on your home computer, and then used Git to create a local repository.

The following steps describe how to push your local Git repository to your DreamHost server. In this way, you can develop your website on your home computer and push the changes to your live DreamHost website.

Creating an SSH key pair

  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:
    [local ~]$ ssh-keygen -t rsa -b 4096 -C "DreamHost Git repo"
    
  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
    dreamhost-git-key
    dreamhost-git-key.pub

Uploading the public key to your DreamHost server

  1. Use the following instructions to copy 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/pass your website is under. View the following articles to locate this information:

    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
  4. Create a new directory for your new remote repository on your DreamHost server. If this code is meant to replace your existing website, you could name it the same as your website ending in .git.

    Since this is a remote repository and not a working repository, the directory name you create must end with .git. You can then checkout this repository in the future to work on it.

    [server]$ cd ~
    [server]$ mkdir example.com.git
    [server]$ cd ~/example.com.git
    [server]$ git init --bare
    [server]$ git symbolic-ref HEAD refs/heads/main
    
  5. Run ls -la to confirm the files and directories have been created.
    [server]$ ls -la
    HEAD
    /branches
    config
    description
    /hooks
    /info
    /objects
    /refs
  6. 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.

Creating a Git repository 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
    [local ~]$ git config --global init.defaultBranch main

    The last line ensures newly created repositories use the name main as the primary branch. View the following article for further details:

    This creates a .gitconfig file under your username.
  2. 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"
  3. Navigate to your git repository on your local computer, and then push to the remote repository on your DreamHost server. 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 github.com.

    [local ~]$ git remote add dreamhost ssh://user@server.dreamhost.com/~/example.com.git
    [local ~]$ git push -u dreamhost main
    
  4. Check your DreamHost server to confirm the repository was correctly pushed by following the instructions in the next section.

How can I confirm my repository was correctly pushed to my DreamHost server?

If you run git status in the ~/example.com.git directory, the following error appears:

[server]$ git status
fatal: This operation must be run in a work tree

This is because it's a 'bare' repository, not a working directory you can use. You could clone this remote repository into a working Git repository. This allows you to view the files that were pushed to the server.

  1. Log into your DreamHost server via SSH.
  2. Clone the repo to a new location by running the following in your user's directory:
    [server]$ mkdir ~/cloned.example.com.git
    [server]$ git clone ~/example.com.git ~/cloned.example.com.git
    [server]$ cd ~/cloned.example.com.git
    [server]$ ls -la
    
    This cloned directory now contains all the files you pushed from your local machine.

How can I use Git for web development?

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

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?