How to migrate part of a Drupal project to a new project repository

This is a writeup of a problem I encountered when working on the CollectiveAccess Drupal module: I had been maintaining a submodule called CollectiveAccess Feeds within the main Drupal CollectiveAccess project, but I found out that it would be more practical to have this as a separate module, in order to move it faster then the CollectiveAccess module itself, which is already more stabilized.

Below are the steps I took to migrate this module to a new Drupal project. It might not be the most efficient or perfect solution, but it worked for me. If you know of better ways to achieve this, feel free to leave a comment.

Structural situation

To make my explanation more clear, here is the start situation:

* collectiveaccess
    |_ collectiveaccess.info
    |_ collectiveaccess.module
    |_ ... (more collectiveaccess module files)
    |_ modules
       |_ collectiveaccess_feeds
          |_collectiveaccess_feeds.info
          |_collectiveaccess_feeds.module

Here is what i wanted it to be:

* collectiveaccess
    |_ collectiveaccess.info
    |_ collectiveaccess.module
    |_ ... (more collectiveaccess module files)

* collectiveaccess_feeds
    |_collectiveaccess_feeds.info
    |_collectiveaccess_feeds.module

The following commands assume you have git maintainer access for the original Drupal project, and have created (or maintainership over) a second Drupal project, specifically for the spin off module.

Steps to achieve this:

Note: obviously you would have to change the project names for your own scenario!

  1. Create a working directory:
    mkdir ~/migration
    cd migration
    

  2. Clone the current project:
    git clone --branch 7.x-1.x [drupal.org git username]@git.drupal.org:project/collectiveaccess.git
    

  3. Create a separate project for the spin off module:
    git clone --no-hardlinks collectiveaccess collectiveaccess_feeds
    

  4. Filter the spin off module repository so only this module remains:
    cd collectiveaccess_feeds
    git filter-branch --subdirectory-filter modules/collectiveaccess_feeds HEAD -- --all
    git reset --hard
    git gc --aggressive
    git prune
    

  5. Still in the directory of your spin off project, we link the project to drupal.org:
    git remote rm origin
    git remote add origin [drupal.org git username]@git.drupal.org:project/collectiveaccess_feeds.git
    git push origin 7.x-1.x
    

  6. This step only applies if you have multiple branches:
    remove the spin off module directory
    cd ~/migration
    rm -rf collectiveaccess_feeds
    

    Now prepare yourself to repeat these steps for the 6.x-1.x branch (or another)

    cd collectiveaccess
    git checkout 6.x-1.x
    cd ~/migration
    

    Now repeat steps 3 to 5 for this particular branch. Repeat again for each branch you might have.

  7. Clean up original repository
    remove all git clones in your migration directory & do a new clone:
    rm -rf ~/migration/*
    git clone --branch 7.x-1.x [drupal.org git username]@git.drupal.org:project/collectiveaccess.git
    cd collectiveaccess
    

  8. Remove the spin off code from the original project
    git rm -rf modules/collectiveaccess_feeds
    git add -A
    git commit -m "remove spin off code"
    git push origin
    

    Repeat this step for your other branches. Switch to them via the following command:

    git checkout 6.x-1.x
    

References used: http://stackoverflow.com/questions/359424/detach-subdirectory-into-separate-git-repository

Comments

...and I had to migrate all the history from the original project too. Here's what I did:

http://drupal.org/node/1335196