Lightweight Deployment With Git

I’ve used this setup so many times now that I wanted to write up a list post explaining how I do it. This method is something I found on this site, but I just wanted to add my own commentary.

The idea is that you are developing a website locally using git, and you want to be able to easily push to your live site with git. We will set up a remote repository on your server, and push to it.

Let’s say you have your local repository already set up. Log on to your remote machine. Navigate to a directory where you want to keep your repository. This does not necessarily need to be the same location as your code, and you actually probably want it to be a different place.

### On the remote machine
corn03:/afs/ir/group/paperless2> mkdir repo.git && cd repo.git
corn03:/afs/ir/group/paperless2/repo.git> git init --bare
Initialized empty Git repository in /afs/ir.stanford.edu/group/paperless2/repo.git/

The way this is going to work, is that we are going to create a post-receive hook. A post-receive hook means you can run some script after this repo has received a push. What we are doing here is checking out the current code to some directory, which we define as the GIT_WORK_TREE. You can make the GIT_WORK_TREE wherever you want. Then we make the script executable.

corn03:/afs/ir/group/paperless2/repo.git> cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/afs/ir/group/paperless2/cgi-bin git checkout -f
corn03:/afs/ir/group/paperless2/repo.git> chmod +x hooks/post-receive

Now, on your local machine, add the remote you want to push to. On this first one, make sure you include the branch you are including, like master.

## Locally
> git remote add web ssh://jkeeshin@corn23.stanford.edu/afs/ir/group/paperless2/repo.git
> git push web master

For any future updates

> git push web

It’s pretty basic, easy to use, and works for lightweight deployment for a site by yourself or with a few other people.

The only issue I had was one time, my internet connection went down in the middle of deployment and the git process crashed. Then the next time I tried to push, nothing happened. After a little bit of searching, the fix was that there was a file “index.lock” that was created, and once we removed that file, it worked again.

3 thoughts on “Lightweight Deployment With Git

  1. Any chance you could fix the Facebook bookmarklet you posted about back in August? The javascript has vanished.

    Or at least update the post so that people (like me) quit trying it until it’s fixed?

    Thanks!

  2. Hey Jeremy, I think git is quietly one of the most useful, multi OS, pieces of open source freeware out there. Never thought to use it like this, but it makes perfect sense. Thanks.

  3. @Rich – I’ve just updated the bookmarklet and so it works again.

    @Perry – Yeah, git is pretty awesome. The more I use it the more I find out how much there is to it.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>