Refactoring a directory into a git submodule

16 Aug 2013, 1 minute read

To release the Rsnapshot Puppet module I needed to extract the module directory from our entire Puppet configuration Git repository (we know it’s wrong, but it’s how we started and are moving away from it slowly). Fortunately Git has some great functionality (hint: it’s submodules) to allow the directory to be pulled out into a new repository, keeping the commit history.

The steps I wanted to achieve were:

Create a new repository containing only the Rsnapshot directory (and the commit history for that directory).

Remove the directory from the Puppet repository and replace it with a pointer to the Rsnapshot repository.

Git submodules solve this use-case. A submodule is treated as a separate repository, but resides within the parent repository’s directory structure.

The simplest way to filter the repository is to clone it, apply the filter to the clone and then push the the filtered repository to your origin server (if you use one). The following example assumes you’ve created an empty repository ready for the submodule: