Skip to content

Instantly share code, notes, and snippets.

@alexanian
Last active September 27, 2020 14:22
Show Gist options
  • Select an option

  • Save alexanian/46bec5ae9d90757f4253066ca743f3fe to your computer and use it in GitHub Desktop.

Select an option

Save alexanian/46bec5ae9d90757f4253066ca743f3fe to your computer and use it in GitHub Desktop.
How to split a repository's subdirectories into their own repo

Okay, so you made a mistake. You thought you should bundle a bunch of your code into a single repository because these disparate apps shared a few libraries and now you're stuck with a swollen monster repo that is a nightmare to deploy from.

(At least... I made this mistake.)

Anyway. You want to split those into multiple repositories and, in the process, regain a shred of your sanity. Let's say you have this repository structure:

monster_repo
│   
└───project1
│   │   file011.txt
|   |
│   └───subfolder1
│       │   file111.txt
│       │   ...
|
└───project2
|   │   file021.txt
|   │   ...
|
└───shared_lib
|   │   ...
│   
└───test
│   │
│   └───project1.tests
|   |    |   ...
|   |
│   └───project2.tests
|       |   ...
|

and you want to separate the two projects. Here's what you're going to do:

  1. Clone the repository into whatever you want your subdirectory repo to be called:

     git clone https://github.com/you/monster-repo ./project1-repo
    
  2. Forget the old repo's history, you're free now:

     git remote remove origin
     git tag | xargs git tag -d
     git gc --prune=now
    
  3. Deploy this ridiculous git incantation that I can't say I fully understand:

     git filter-branch --index-filter \
     'git rm --cached -qr --ignore-unmatch -- . && git reset -q $GIT_COMMIT -- project1 test/project1.tests shared_lib' \
     --prune-empty -- --all
    
  4. Profit (by which I mean deal with all the failing tests now that the directory structure is changed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment