Integrating git with your existing website
Tyler Cipriani Posted

Better git it in your soul*.

Git has a lot of great tutorials for getting started. There are also a number of great articles on how to use git and github for your workflow.

What I haven’t seen is an article on how to integrate git with your current site without storing any code on github. I’m writing this blog to create a quick reference for how to get up and running using git on your existing site.

Needed

I’m making the assumption that you have the following:

I’m using Ubuntu 12.04 locally, but I’d assume that most of this won’t be too different on a different distribution or on a Mac—but I’m probably totally wrong about that ☺


Step One:

RSync your site to your local development environment

In order to begin to develop locally (and break the old cowboy-coding habits that you’ve undoubtedly developed over the years) you need a local copy of your site.

  1. Open your terminal emulator and cd to the directory in which you will be storing these files (i.e. cd /srv­/www­/tylercipriani.com­/public_html)
  2. Rsync the htdocs or public_html from your webserver into this local directory:

The command breaks down like this:

  • a means “Archive”—keeps permissions, mtimes, etc the same
  • v means “Verbose”—increases verbosity of the command
  • e means “RSH”—allows you to use remote shell (same as RSH=command)
  • :/path/to/htdocs/—the path to you htdocs folder. The trailing / is significant—it means copy the content of the htdocs directory rather than the directory by name
  • . is the current directory

Step Two:

Initialize git in local development environment.

This step will create a new git repository on your local machine and add all the code that you’ve rsynced in the previous step to that repo.

  1. ­cd to the directory to which you previously rsynced your site and initialize a git repository by running git init

  2. Add the contents of the current directory to the git repository by running git add .
  3. Commit all your newly added files to the repo by running your first commit git commit -m “First Commit”


Step Three:

Setup a bare repo on your web server

You need a bare repo out on your webserver that will act as a mirror to your local development environment.

  1. ssh into your webserver and make a new directory, I usually make it above the webroot (i.e. htdocs)

  2. Once inside the new directory initialize a bare repository by using the --bare flag:

  3. Now we can define a new post-receive hook that will be triggered whenever an update is pushed to this new bare repository. The post-receive hook can be any type of script you want, the script below is written in bash. cd into the .git/hooks directory and create a file called “post-receive”. Copy the code below into the file:

    make sure that this code is executable by running chmod +x .git/hooks/post-receive


You’re Done!

Push to your new repo, you beautiful command-line ninja, you!

Back on your local machine, in the webroot of your local development environment, add your bare webserver repo as your remote and push your git repo up to your server. The post-receive hook will take care of the rest!

By using the -u flag you’re setting the upstream which means you can just run git pull without further arguments to merge origin and master.

Oct 2012
S M T W T F S