dead simple git deployment

This is more of a reference for myself, but here's the bare minimum on how to set up a remote git repository on a server, push to it, and then get something running. If you ever find yourself doing annoying shit like sending lots of files around on FTP everytime you want to update something on your server, try this out. Git will only send the changes (deltas), so you'll be sending much less data around.

You can substitute setting up the serverside git repo by just using Github, but then you can't have private repositories without paying. If you already have a VPS or server of your own, you should use this to deploy content to it. I use this method to deploy my jekyll blog (what you're reading now) as well as other side projects.

Let's begin...

prerequisites

Make sure that you have git installed on both your own computer as well as your server. Install by downloading the latest version, and then installing according to the instructions.

server side

After logging into your server, using ssh, create a folder for your repository to live in:

mkdir -p /srv/git/project

Of note, mkdir -p will create any intermediate directories, such as /srv/git/, if they don't already exist. Now, initialize a bare git repository in that folder:

cd /srv/git/project
git init --bare

We'll also want to add a hook to this git repository. Think of a hook as a listener, that will perform an action upon some git-related event. In our case, we'll want to create a clone of the (remote) repository on the remote server once the repository received changes, so that the files will be accessible to a web server, meaning we should create a post-receive file in the hooks directory of the repository:

vim /srv/git/project/hooks/post-receive

In this file, add some simple logic to clone and copy your repository to your web directory:

#!/bin/sh
GIT_REPO=/srv/git/project
TMP_GIT_CLONE=/tmp/project
PUBLIC_WWW=/srv/www/project

git clone $GIT_REPO $TMP_GIT_CLONE
cp -rp $TMP_GIT_CLONE/* $PUBLIC_WWW
rm -rf $TMP_GIT_CLONE

Whatever you put in this file will be run as a regular shell script every time you commit to this repository. You can imagine doing things like restarting your server when you push, emailing someone, etc. By changing the shebang at the top of the file, you can run python or other scripts. The sky's the limit.

Make sure to also make this script executable, since we want git to be able to run it after you push changes:

chmod +x /srv/git/project/hooks/post-receive

client side

Now, we've configured the server. Go back to your own machine, where you've been working on your project.

If you haven't already, initialize your project's own local repository:

cd /path/to/your/project
git init

Add your server as a remote repository, filling in your own server details:

git remote add origin ssh://username@yourserver.com/srv/git/project

You can check whether your configuration worked by seeing what git show origin tells you. Now, you can commit your changes, push to the server, and everything should just work.

git add .
git commit -m "first commit, bro"
git push origin master

If you're tired of typing your password, you should set up passwordless ssh. Other than that, you now have a git-based workflow that deploys your goodies to your server with very few keystrokes.