Overview
With Git, you can manage your local website or application code and push it to your DreamHost server. This setup lets you develop entirely on your home computer while using your DreamHost server as a remote server backup for your code.
Using Git for web development
The instructions in this article only back up your local repository to a DreamHost server. They do not update your live website. See this article for instructions on how to push changes on your local computer to your live website.
How do I configure SSH keys?
This section explains how to connect to your DreamHost server using SSH keys.
How do I create an SSH key pair?
This step creates an SSH key pair on your computer.
- On your local computer, navigate to your user's ~/.ssh directory:
[local ~]$ cd ~/.ssh
Create the directory if it does not exist.
[local ~]$ mkdir ~/.ssh
- 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 ed25519 -C "DreamHost Git repo" Generating a public/private ed25519 key pair. Enter the file in which you wish to save the key (i.e., /Users/username/.ssh/id_ed25519): dreamhost-git-key
- Enter a name for the file when prompted, such as dreamhost-git-key.
- Enter a secure password when prompted.
- Confirm the new files have been created.
[local ~]$ ls -la dreamhost-git-key dreamhost-git-key.pub
How do I upload the public key to my DreamHost server?
This step copies the public key from your home computer to your DreamHost server's authorized_keys file.
To run these commands, you need the name of your DreamHost server and the user/password assigned to your website. See these articles to locate this information:
Additionally, the DreamHost website username you're running the command with must be configured as a shell user.
- Copy your key to your DreamHost server.
[local ~]$ cat ~/.ssh/dreamhost-git-key.pub | ssh user@server.dreamhost.com "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"
You may receive this 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 mkdir ~/.ssh; 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"
- Log into your DreamHost server via SSH.
[local ~]$ ssh user@server.dreamhost.com
If you're using a custom key name, you may receive an error when attempting to log in. If so, you may need to use ssh-agent. See this article for more information.
- View the contents of the file to confirm your key has been added to the DreamHost server (this should output your key):
[server]$ cat ~/.ssh/authorized_keys
How do I configure the repository?
This section creates a repository on your DreamHost server to store your local website or application code.
How do I create a bare repository on my DreamHost server?
This step creates a bare repository on your DreamHost server. This will be the remote repository you push your code to from your local computer.
- Log into your DreamHost server via SSH.
- Create a new directory for your new remote repository.
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
- Run ls -la to confirm the files and directories have been created.
[server]$ ls -la HEAD /branches config description /hooks /info /objects /refs
- Confirm the HEAD file is set to use main by viewing its contents.
[server]$ cat HEAD ref: refs/heads/main
How do I configure Git on my computer?
These steps should be run on your local computer.
- Create your identity to be used with Git. These commands create a .gitconfig file under your username.
[local ~]$ git config --global user.name "John Doe" [local ~]$ git config --global user.email johndoe@example.com
- Confirm your Git version.
If your local Git version is at least 2.28, run the init.defaultBranch command to ensure all newly created repositories use the name main as the primary branch.
[local ~]$ git --version
[local ~]$ git config --global init.defaultBranch main
If, for some reason, you're unable to upgrade your version to at least 2.28, you can still manually change the primary branch name to main after you've made the first commit below. See this article for further details.
How do I create my local Git repository?
These steps create a Git repository for your website on your home computer. You'll then use this to push its contents to your DreamHost server.
- Navigate to your project directory.
[local ~]$ cd /home/user/project-directory
- Initialize the repository.
[local ~]$ git init
- Add all contents and create your first commit.
[local ~]$ git add . [local ~]$ git commit -m "First Commit"
How do I push my local repository to the remote repository?
This step pushes your local repository to your DreamHost server.
- 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
- Check that the new remote exists.
[local ~]$ git remote show dreamhost
- Push the local repository to your DreamHost server.
[local ~]$ git push -u dreamhost main
How do I confirm my repository was correctly pushed to my DreamHost server?
- Log into your DreamHost server via SSH.
- 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
- Clone the repo to a new location by running these commands, 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 ~/cloned-example.com.git
This new directory now contains all the files you pushed from your local machine.