Last Updated: February 25, 2016
·
825
· webrgp

ExpressionEngine and Git: Using Git Hooks to Track Database Changes

Step 1: Setup a git repo

Once you have installed your lincensed copy of ExpressionEngine in your local machine, add a directory named DBSchema to the root of the site. Jump in the terminal and initialize a git repository in it:
cd ~/projects/new-ee-website git init

Step 2: Create and enable hook

After initializing your repo, go to the hidden .git directory, where you are going to find a hooks directory. In there create a new file named pre-commit , and make it executable:
cd .git/hooks/ touch pre-commit chmod +x pre-commit

Step 3: Add bash script to hook file

Then edit the newly created pre-commit file and add the following code:
!/bin/bash DBUSER="db_username" DBPASS="db_password" DB="db_name" SCHEMAPATH="DBSchema" /Applications/MAMP/Library/bin/mysqldump -u $DBUSER -p$DBPASS $DB > $SCHEMAPATH/$DB.sql git add $SCHEMAPATH/$DB.sql exit 0

That's it! What its happening is that we told git that before every commit, run a mysqldump of the database and add it to the stage to be committed.

Note: I would recommend using this only in development, when working in teams before turning over for content or to the client.