Convert subfolder into Git submodule

by @im_a_muppet on January 13, 2011

Recently I had the need to refactor a large repository we use at work. The layout of the repository was as follows

~ $ cd ~/Development/LUF
  (master) Development $ ls
  src/
  (master) Development $ cd src; ls
  AAR/ MSEL/ SigEvServer/ WebServices/
  (master) src $

All the apps in the src folder were Rails app (except AAR) and I wanted to move each Rails app to its own repository and then include those repositories as submodules with in the src folder.

Step 1. Clone new repositories.

You will need a new repository for each one that you are moving. Since we want to keep the history of each subfolder we need to clone the repo as follows.

  
  ~ $ cd Development
  Development $ git clone --no-hardlinks LUF MSEL
  Development $ git clone --no-hardlinks LUF SigEvServer
  Development $ git clone --no-hardlinks LUF WebServices

Step 2. Filter out the files you want to keep and remove the others.

Now we will move all the files we want to keep up to the top level folder. Reset the head and then prune out all the other files. We will also remove the origin too since they will no longer be compatible.

  ~ $ cd Development/MSEL
  (master) MSEL $ git filter-branch --subdirectory-filter src/MSEL HEAD -- --all
  (master) MSEL $ git reset --hard
  (master) MSEL $ git gc --aggressive
  (master) MSEL $ git prune
  (master) MSEL $ git remote rm origin

Repeat the process for SigEvServer and WebServices

Setp 3. Push the new repositories to the upstream server.

Now that you have local copies of all the repositories that you wanted you need to push them to the upstream server so everyone can use them.

  ~ $ cd Development/MSEL
  (master) MSEL $ git remote add git@github.com:wmernagh/MSEL.git

Repeat the process for SigEvServer and WebServices

Step 4. Add the new repository as submodules to the original repository

The final step is to remove the folders from the initial repository and add in the missing folders as submodules.

  ~ $ cd Development/LUF
  (master) LUF $ git rm src/MSEL src/SigEvServer src/WebServices
  (master) LUF $ git commit -m "Removing the folders that are now repositories"

  (master) LUF $ git submodule add git@github.com:wmernagh/MSEL.git src/MSEL
  (master) LUF $ git submodule add git@github.com:wmernagh/SigEvServer.git src/SigEvServer
  (master) LUF $ git submodule add git@github.com:wmernagh/WebServices.git src/WebServices

  (master) LUF $ git submodule init
  (master) LUF $ git submoduel update
  (master) LUF $ git add .gitmodules src
  (master) LUF $ git commit -m "Added in submodules for removed folders"

That’s it.

blog comments powered by Disqus
Tweet