How to push your Git repository to an external server

It's always a good idea to keep a copy of your repository on an external server in case your local copy is deleted. There are two general ways you can go about this:

  • Create your application and Git repository on your home computer. Once it's ready, push the repo to your DreamHost server.
  • Create your application and Git repository on your DreamHost server. Then, you can push to GitHub

These two general options are detailed below.

Pushing your DreamHost repository to GitHub

If you've developed your site on your DreamHost server, it's recommended to put a copy on GitHub for two main purposes:

  • This creates an external backup of your code, and
  • It makes future collaboration with other developers much easier.

Follow these steps to push your repository to GitHub

  1. Sign up at GitHub.
  2. Create a public or private repository at GitHub.
  3. Log into your DreamHost server via SSH and navigate to your application folder where you set up Git.
  4. Run the following command to push your app to GitHub:
    [server]$ git remote add origin https://github.com/<username>/<app_name>
    View the following links for details:
  5. Push your repository to GitHub
    [server]$ git push origin master
    View the following link for details:

Pushing your local repository to DreamHost

These directions assume that you've created your application on your home computer, and then used Git to create a local repository. The following steps guide you through how to push your local Git repo to your DreamHost server.

Pushing from Linux

  1. Setup SSH keys.
    [local ~]$ ssh-keygen -t rsa -b 4096 -C "DreamHost Git repo"
    
  2. Use ssh-copy-id to install your public key into your DreamHost server's 'authorized_keys' file.
    [local ~]$ ssh-copy-id -i ~/.ssh/id_rsa.pub user@server.dreamhost.com
    

    You can also use the command below in the Pushing from OS X section to copy your public key to your web server.

     

  3. Log into your DreamHost server via SSH:
    [local ~]$ ssh user@server.dreamhost.com
    

    If you receive an error when attempting to login you may need to use ssh-agent. View the following article for details:

  4. Create your local repository.
  5. Create a bare remote repository on your DreamHost server:
    [server]$ mkdir project.git
    [server]$ cd project.git
    [server]$ git init --bare
    [server]$ exit
    
  6. Back on your local computer, push to the remote repository on your DreamHost server. Make sure to change 'user' and 'server' to your actual DreamHost username and servername:
    [local ~]$ git remote add origin ssh://user@server.dreamhost.com/~/project.git
    [local ~]$ git push origin master
    
    You should see a response as follows:
    Counting objects: 3, done.
    Delta compression using up to 2 threads.
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 200 bytes | 0 bytes/s, done.
    Total 3 (delta 0), reused 0 (delta 0)
    To ssh://username@server.dreamhost.com/~/project.git
    * [new branch] master -> master
    

Pushing from OS X

The instructions for OS X are the same as the ones for Linux, except step #2 – instead of using 'ssh-copy-id', use the following command to copy your public key to your DreamHost server:

[server]$ cat ~/.ssh/id_rsa.pub | ssh user@server.dreamhost.com "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"

Then, proceed with all the other steps.

Pushing from Windows

  1. Download Git for Windows:
    This example uses 2.5.0.
  2. Navigate to your project folder.
    01 Git on Windows.png
  3. Right click and then select 'Git Bash Here'.
    This opens up a terminal that's already running Git.
  4. Run the following to create your user and email to be used with Git:
    [local ~]$ git config --global user.name "John Doe"
    [local ~]$ git config --global user.email johndoe@example.com
    
    This creates a .gitconfig file under your username.
  5. Run the following in your project directory:
    [server]$ git init
    [server]$ git add .
    [server]$ git commit -m "First Commit"
    
  6. Add the remote repository.
    [local ~]$ git remote add origin ssh://user@server.dreamhost.com/~/project.git
    [local ~]$ git push origin master
    
  7. Check your DreamHost server to confirm the repository was correctly pushed by following the instructions in the next section.

Confirming your repository was correctly pushed to your DreamHost server

  1. Log into your DreamHost server via SSH.
    When you run 'git status' in the /project.git directory, you'll receive the following error because it's a 'bare' repository:
    fatal: This operation must be run in a work tree
  2. Clone the repo to a new location by running the following in your user's directory:
    [server]$ mkdir clonedproject.git
    [server]$ git clone $HOME/project.git $HOME/clonedproject.git
    [server]$ cd clonedproject.git
    
    This directory contains all the files you pushed from your local machine.

See also

Did this article answer your questions?

Article last updated .