Drupal and GIT

May 10, 2009

During this year I've been moving my projects to GIT. While my private projects are on this server, my public ones are on GitHub as Open Source. Git is a really wonderful version control tool for any kind of project. It makes it easy for me to deploy, maintain and update my current sites. I just wish there could be a way to include the database on it. In this post I will explain how I'm using GIT to manage a Drupal site on both local and live environment. And since most people already have a site working, I will explain starting from an existing site.

Create the repository

The very first thing is going to the actual site, in my server this site is in: /var/www/sitename.com/public

cd /var/www/sitename.com/public
git init
echo "sites/default/settings.php" >> .gitignore

You can also run this if you want to ignore the files folder (that's really up to you)

echo "sites/default/files" >> .gitignore

Then you have to add all files to the repository and make the first commit

git add .
git commit -m "Initial commit for this site"

Making the site remotely available

Once you have your local repository working on your server it's time to push this to another place. This is asuming that you have a 'git' user somewhere else and you have access to SSH. I use my own server so I can actually change the address to localhost

scp -rp .git git@yourreposerver.com:sitename.git

The last line is really dependent on your current config. In my case I use this:

scp -rp .git git@ivansotof.com:sites/sitename.git

You can google a little about SCP command if you are not familiar with it. It will ask for a password since it's using SSH for copying the .git folder. It will take a while copying everything up to that server but when it's done you just need to add a remote entry to the local repo.

git remote add origin git@yourreposerver.com/sitename.git

And you are done copying the site.

Cloning and working on your local environment

Now that we have the live site under version control and the repository available for cloning you just need to clone it to your working environment.

git clone git@yourreposerver.com:sitename.git

It will create a folder sitename/ that will be a perfect copy of what you have on your live site. Now you can just go in and make changes to the site:

cd sitename

If you are using TextMate on OSX or E-TextEditor on Windows you can open the working folders from here. If you are not, then you should :) On OSX:

mate sites/all/

On Windows

e sites/all/

When you are done making changes:

git commit -m "Making some random changes"

Now push all changes to the remote server

git push origin master

Now go to the "live" site and just do:

git pull origin master

You should now have all latest changes working on your remote server. This is so far the setup I am using on most of my sites. I didn't include copying the database but I hope the readers already have some experience on that. Since I'm ignoring settings.php you should be able to have a configuration working on any copy of the site. If you are using a different setup, please share it :)

Jim
Anonymous's picture
on May 11, 2009 Thanks for the info. I'm trying to decide now between GIT and SVN. Added to DrupalSightings.com
Tom Bamford
Anonymous's picture
on May 16, 2009 Sweet some more awesome stuff by you!! some nice little reminders for me! I agree completely about the automated db dumps into the repository, been trying to work out a way of doing that for ages. only possibility is manually using the backup module, but that doesn't streamline it into the process :( Tom
on May 19, 2009 Well, Drupal 7 is supposed to support SQLite so maybe that will be good news for us. I haven't tried yet, but definitely we need a way to version contorl databases.
on Dec 01, 2009 I haven't moved from svn to git yet, but I do have a way to keep my database under version control: http://drupal.org/project/dbscripts.

Post new comment

You can use your Gravatar account.

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.