Create a GIT Remote Endpoint to Publish Your Site

This generic example uses Dreamhost to demonstrate. Some info may change from server to server.

Set up the repository on the server

SSH into your host.

ssh [user]@[host]

You need to get to the point where you don’t need to enter a password anymore… You may need to restart terminal.

Make sure you at your account root or one directory above the live site directory.

cd ~

Make your GIT directory and move into it.

mkdir [yourdomain].git && cd $_

Create an empty GIT repository.

git init --bare

Create/Edit the post-receive hook in order to update the files when pushed to.

vi hooks/post-receive

Enter this into the post-receive file:

#!/bin/bash

# Replace this line with your real domain name
DOMAIN=[yourdomain]

echo "-= Transferring changes to $DOMAIN =-"

# Clearing git env
unset GIT_DIR
unset GIT_WORK_TREE
cd ~/$DOMAIN
git pull

echo "-= Done =-"

Set the permissions on post-receive file.

chmod +x hooks/post-receive

Change back to the parent directory.

cd ..

Clone the GIT repository into the site.

git clone [yourdomain].git [yourdomain.com]

Exit the SSH session.

exit

Add your SSH public key to the server

If you have not generated a public key before, you may need to create one.

ssh-keygen -t rsa

Add to authorized keys on the server.

cat ~/.ssh/id_rsa.pub | ssh [user]@[host] "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"

Add your new remote into your local GIT repository

Change to your GIT repository.

cd [git repository path]

Add the remote. Here we are using “live” as the remote name, this could be anything. eg: “production”, “staging”…

git remote add live ssh://[user]@[host]/~/[yourdomain].git

Pust to test it out.

git push live master

Remove public access to .git

Keep the public from viewing the .git and .gitignore files in the new repo. You can place this in an .htaccess in the parent directory to affect multiple sites at once, or right into the main site .htaccess. This very simple rule returns a 404 error when trying to access anything starting with “.git”. This removes access and effectively hides the fact that they even exist.

RedirectMatch 404 /\.git

Sources

  1. Automatic Git deploys on Dreamhost
  2. How do I prevent apache from serving the .git directory?
  3. Deploying on Dreamhost Using Git