<![CDATA[I've recently run across an interesting challenge in some historical source code repositories. These repositories did not begin their lives in Git and are arranged so that independent products are in sub-directories under a root parent directory. This means, any time anyone clones the repository they get a ton of unneeded source code that goes along with it.
Worse, our build solution triggers Continuous Integration (CI) based on repositories, not directories. So, each time any commit is made to the master branch and pushed to the remote server all builds pointing to the repository are triggered.
Less than optimal.
My challenge was to find a way to extract a sub-directory, keeping its change history intact, and convert it to its very own repository. This turned out to be simpler than I had anticipated.
Here are the command line commands needed to invoke this bit of wizardry for yourself.
- Open a command console. I use Windows, so my commands will have a distinct Microsoft accent to them. 😉
- Change to the root directory in your source code repository.
- cd \git
- Create a new directory to contain your new repository
- md betterThanChocolateRepo
- Change to the new directory
- cd betterThanChocolateRepo
- Clone the entirety of your existing repository into the new directory
- git clone http://git-server/CandyFactoryRepo
- Use the ‘filter-branch’ command in Git to extract the directory of interest and it’s commit history.
- git filter-branch –subdirectory-filter /CandyFactoryRepo/betterThanChocolateRepo
- Now, connect the filtered result to the new remote repo
- git remote set-url origin http://git-server/betterThanChocolateRepo
- Push to the server
- git push