Git Submodules

Today I was working on a Drupal site, adding the CKEditor WYSIWYG editor into my code base for a new site deployment.  It's a step I have to do each time I deploy a site, and each time there is a new version of CKEditor that I want.  Wouldn't it be neato if I could just add the git repo for the CKEditor project to my project?  Then I could just pull new versions directly in with a few GIT commands?

Hello GIT Submodules!

The process can't be much simpler.  When we add a submodule, we actually add a pointer to a tag branch, along with any customizations we make to that branch.  When we update to a new tag, GIT merges the upstream changes with our customizations - every time.  Nice.

No more copy and pasting.  No more finding out what the new version is and downloading.  Just GIT it.  



Adds a git repo as a submodule

  • git submodule add <<url>> <<folder>>

Within the folder, use git to select the tag branch to use

  • git tag
    - list all tags 
  • git checkout <<tag>>
    - checkout the tag you want to use

Now your project repo will store the folder and a pointer to the git submodule repo via the .gitmodules file.  Be sure to commit this to your git project. 

Cloning a project with Submodules

When you clone this project, you will need to take a few extra steps:

Clone the project as you normally would:

  • git clone <<<url>>

Now initialize the local config file:

  • git submodule init

Finally, update the submodules to pull the current code base:

  • git submodule update

Your repo will be ready to go!

Common Drupal Libraries and their GIT sources

git submodule add bad-behavior
git submodule add htmlpurifier