Patrick Avella

Programmer, Web Developer, Blogger

How To Work with Git and Drupal

5/23/2011 -- Patrick

With the resources in this article you should be able to create a remote Git repository, track a Drupal installation, deploy multi sites quickly, work more collaboratively, and keep better backups. Git is a command line utility to manage code repositories. Drupal is a full featured content management system with a flourishing community and code base. Using Git and Drupal allows a web developer to rapidly create, deploy, clone and backup full featured media rich web 2.0 websites. This guide assumes you've already learned the basics of managing a Drupal installation, and are interested in learning to use Git with Drupal.

Starting Git

Git by Example: An easy to follow guide that helps you set up your first Git repository. This walks through cloning or initializing your first repository, making changes to code, and applying those changes in a controlled revision environment.

$ # COMMON COMMANDS
$ # Grab a copy of a project
$ git clone ssh://myuser@myhost.com/myrepo.git

$ # Make sure you're on the write branch. See git show-branch
$ git checkout master

$ # Commit changes
$ git commit -am"My commit message"

$ # Roll back changes. See git ref-log for hash
$ git reset --hard [hash]

$ # Update locally or remotely
$ git pull origin
$ git push origin

Handling the new .gitignore file in D7 and D8: Git is the preferred code management solution for Drupal, to the extent that Git only features are provided right in newer versions of Drupal. All the more reason that you should be using Git with Drupal.

Git Drupal Workflow

Before Git can be truly useful, you need to understand the concept of a Git Drupal work flow. Typically you'll keep your own, working local copy of the Drupal installation and use Git to git pull other people's changes, or git push your changes back to the main repository.

Drupal.org Git workflows: Drupal.org is a great source of information, and it's recommended that you read the documentation that it makes available.

Using Git to Control a Drupal Multi Site Setup: Drupal's enterprise class multi site management allows for migration and scaling of many websites sharing the same Drupal core. This allows for streamlined site maintenance and updates. This Git Drupal work flow explains how to deploy Drupal multi sites with Git.

Drupal Database and Dependency Management with Git

Git will handle a lot more than code and images. Git can track and merge database text dumps as well. Since Git repos are zipped between revisions, it offers a simple and efficient way to keep a collection of restore points.

$ #Dump a database as individual lines for merging
$ mysqldump -umyuser -p --skip-extended-insert --skip-comments mydatabase

Backup Your Database in Git: Here's an easy to follow guide that walks through tracking a mysqldump with Git. You could automate this process even further by learning about Git Hooks, an API for kicking off routines when repositories are pushed or pulled.

Using Git submodules will allow you to create repositories that can be managed as an individual piece of a larger project. This can have multiple uses with Drupal. For example, each Drupal multi site's resources can be wrapped in its own submodule, or complicated libraries can be managed as smaller logical segments.

$ #Git submodules
$ git submodule init
$ git submodule update

Git Submodules, adding, using, removing, and updating: A thorough guide that gives code examples for everything you might need to do to get started with sub modules.

Submodules: Another guide that covers using Git submodules. This guide also talks about some of the pitfalls of using submodules.

Git will make you more productive with Drupal

There is a certain performance boost that can be gained both in solo and team environments. Drupal lends its self to collaborative work, and Git makes organizing and tracking development much easier. The ability to create, deploy, and update Drupal sites at lightening speed gives you the ability to focus on the important parts of your projects, speeding development time.

Leave your own links and advice in the comments below.