Version Control For Magento Using Git

back to tech articles
Magento CE 1.7.0.2, Git 1.7

There are tons of VCS (What is VCS) tools available these days, but I love to harness the additional power of Git to deploy changes as well as handle version control. With that in mind, I’d like to share a system I use to control site updates/deployment using Git. The CMS of choice today is the ecom beast, Magento. Right, off we go.

The Scenario

We have two sites, mydomain.com and staging.mydomain.com. One is our live production site and the other is our staging (testing) site. They access a separate database each.

The development team (myself included) develop the codebase, including extensions, theme tweaks, etc. on the codebase.

The social, design and marketing teams all work on the Magento site via the admin area. They can install extensions on both servers.

The Challenge

We need our code changes to be pushed to both sites. We also need to capture the media files, extensions and tweaks that are implemented on the live site and pull them to our local codebase for incorporation and deployment to staging.

The Solution

Halt, an image is needed!

Magento In Git

Ok, so here’s how it works.

For each site, there are 2 repos. I will use mydomain.com as an example. One repo sits on the server at /var/www/repos/mydomain.com.git and is a bare repo. There is a post-receive hook in the hooks folder with the following contents:

1
2
#!/bin/sh
GIT_WORK_TREE=/var/www/mydomain/ git checkout -f

This is the repo we push to. On a local machine, this would be configured as live.

The second repo sits at the same level as the site (/var/www/mydomain/) and is set up as origin. This is the repo we pull from to local (generally speaking) to obtain changes from the live server.

An Example

The staging site mimics the live site except that it is referenced as staging for the bare repo and origin_staging respectively.

So, let’s say we want to pull a code change on the live site and push it to the staging site. We would do as follows:

1
2
3
$ ssh root@mydomain.com
$ cd /var/www/mydomain/
$ git commit -a -m "Massive code change on the live site"

On the local machine:

1
2
3
4
5
$ cd /Users/jason/Sites/mydomain/
$ git pull origin master
$ git status
blah blah blah
$ git push staging master

Obviously this test is done with the master branch.

Conclusion

Please comment and enlighten me if you can offer a better solution. In reality, this setup works perfectly and by using the .gitignore files correctly, the repo stays lightning fast and efficient.

Hope it helps you out!