Adding Git to an existing Drupal project

By shane
Fri, 2015-03-13 08:11

Share with Others

I recently received an email from someone who finished reading the 5 Secrets to Becoming a Drupal Ninja eBook. The question was how to set up Git for an already existing Drupal website. The question is a great one because almost everyone that uses Git now had this question at some point.

In this post, we will walk through a few options for how to go about setting up Git on an existing site with multiple Drupal environments (Development and Live). We will also assume that there might be changes on the Dev environment that have not been pushed up to Live yet. We will also assume initially that we will just use a single master Git branch.

If you want to learn more about Git, you can start with An introduction to Git (part 1).

Before you start: Figure out your workflow

Before you go ahead and try to get Git set up, you will want to take a little time to ensure you know how you want your workflow to work. These will vary depending on how your Drupal environments are set up, how many environments you have, and how many developers you might have working on the project. In this case, let's assume this is a relatively simple setup. Just a Development and Live environment with only a small number of developers.

In this case we will want Git installed on the server hosting the Live site as well as the server running the development environment (Note: This could be the developer's own computer). If we walk through the path of a simple new addition to the site, if might look like this:

  1. Changes are made on the development site
  2. Changes are added to Git (using the git add and git commit commands)
  3. Changes are pushed up to a third party Git hosting service (Github, Bitbucket, etc).
  4. On the live site the changes are pulled down (using the git pull command)
  5. If the new functionality changed the database, then the database update script is run (either through the drush updatedb command or through the Drupal /update.php page).
  6. If there are new modules that need to be turned on, they are turned on using the drush en command or by the Drupal Module Admin page.

Your workflow could have more steps than this, but this should give you the idea. By thinking about how code moves through the process initially, you will have a better grasp on how things need to be set up.

This is also a good time to go ahead and set up a new Git repository in your third party Git hosting service. When setting up your new Git repository using one of these services they will often give you the Git commands you will need to run for an existing project.

Git Setup on Existing Site

Note: Make sure you have adequate backups of your Live and Dev sites before getting started... just in case.

If you want Git set up now on all your environments and your Dev environment is not currently ready to go Live, then you will want to start on the Live environment. If you have command line access to the server, and this is a Linux server, you may be able to do this directly on the live server.

Inside the folder for your live Drupal site run the following commands (or the ones that your Git hosting service tells you to... they should be similar). Make sure to replace [git-remote-path] with the path to your hosted Git repository:

git init
git remote add origin [git-remote-path]
git add .
git commit -m "Initial Git commit for my Drupal site"
git push -u origin master

If you refresh the page on your Git hosting service, you should see the master branch with all of the code from your Drupal site. You are now ready to set up Git for your development environment.

In your development environment, I would start by renaming the project folder. If your project folder was currently called drupal7 and was in the /var/www directory on your server, then something like this would work:

mv /var/www/drupal7 /var/www/drupal7_backup

This renames the drupal7 directory to drupal7_backup. You are now ready to clone your Live Git repository:

cd /var/www
git clone [git-remote-path] drupal7

This will clone your live Git repository into a directory called drupal7. If you are following best practices on your development site, the only things that should have changed would all be located in the sites/all folder. If this is the case, then you can simply replace the current sites/all folder in your directory with the one from your drupal7_backup.

rm -r /var/www/drupal7/sites/all
cp -r /var/www/drupal7_backup/sites/all /var/www/drupal7/sites

Also keep in mind that your settings.php file (located in sites/default folder) might need to be copied from your drupal7_backup folder as well.

cp /var/www/drupal7_backup/sites/default/settings.php /var/www/drupal7/sites/default/settings.php

You are now ready to add and commit these changes to your Git repository.

cd /var/www/drupal7
git add .
git commit -m "Adding new features from development"
git push origin master

To pull these changes down to the live environment, just navigate to the project folder and run:

git pull

To pull down any code changes from development (then proceed through the other update steps mentioned above).

Git Wrap-up

That's it. There is really not much to it. From now on, anytime you make changes in your development environment you will follow your predetermined workflow. You will easily be able to view changes to your project code over time and sleep easier at night knowing you have the warm embrace of version control on your project.

Happy Drupal development! Let me know what you think in the comments below.


In case your dev environment has core updates this will not work. In that case you will also want to copy the core files from your backup to the newly checked out directory.

This is exactly what I needed to use GIT & D7!! I have been looking for a cheat sheet like this for a year now!

Thanks CK!

Thanks for your excellent series.

I use git extensively to manage about 15 sites. And love the ability to try things out and then merge the one I like.

What baffles me though is the ability to keep a git repository out of the root of my website. I was using a post hook in a repository below the web root:


# remove any untracked files and directories
git --work-tree=${WEB_DIR} clean -fd

# force checkout of the latest deploy
git --work-tree=${WEB_DIR} checkout --force

But it wiped out the sites folder which I would then have to ssh in and manually copy. Seemed to defeat the purpose.

So my question is twofold. How much of a security risk is a git repository in the root of a site? And is there a better way to post the working tree to a production site?

Post new comment