Overview
Git is an excellent resource to use for web development as it allows you to streamline live updates in addition to providing a copy of your website files.
For example, you can create your website on your home computer and use Git to push a copy of those files to your DreamHost web server. If anything happens to your home computer, you still have a full copy on the web server. You can then configure this web server repository to push live changes to your website.
Using Git to push live changes to your website
First, follow all steps in the following article:
- Pushing your local Git repository to a DreamHost server — Linux & Mac OS X
- Pushing your local Git repository to a DreamHost server — Windows
If you followed all of the steps in that article, your local Git repository on your computer is now copied to your DreamHost server (as a remote repository). But, those changes are just sitting in the remote repository.
If you want your live website to update when you push code to the server, you can add a post-receive hook. This step creates a file to copy updates from your remote server repo to your live site. This example uses a text editor program named nano. View the Creating and editing a file via SSH article for further information on how to edit a file.
- Log into your web server via SSH.
- Make sure you're in your remote repository directory.
[server]$ cd ~/example.com.git
- Run nano and create a file named post-receive:
[server]$ nano hooks/post-receive
- Add this code to the file. It tells where to push these changes. In this example, change username to your actual shell user, and change example.com to your website.
#!/bin/sh GIT_WORK_TREE=/home/username/example.com git checkout -f main
If you changed the web directory for your domain in the past, it may be set to something like /public. In this case, you'd need to add this directory to the file path, as shown below.
#!/bin/sh GIT_WORK_TREE=/home/username/example.com/public git checkout -f main
In these examples, the branch named main is being used. If you've followed along with DreamHost's articles, your local and DreamHost server's repository are set to use this branch already. You should only change this branch name if you want to push a different branch to your live website.
View the following article for instructions on how to configure git to use main:
- Save and close your file to return to your shell.
- Give the file execute rights.
[server]$ chmod +x hooks/post-receive
Now when you push from your local computer's repository to the server, those changes are automatically added to your live website.