Git: chopping out part of the repo into a separate repo

I had to cut out cookbooks/nodejs from the develop into a separate repository and move develop to master.

The problem is, there is a git repository called XYZ with the following structure:

  • cookbooks
    • nodejs ...
    • mysql ...
  • other_stuff ...

and following branches:

  • refs/heads/master
  • refs/heads/develop

I had to cut out cookbooks/nodejs from the develop into a separate repository and move develop to master. Here’s how I’ve done it:

git clone git@github.com:.../XYZ.git .
git filter-branch --tag-name-filter cat --prune-empty --subdirectory-filter cookbooks/nodejs develop
rm -Rf cookbooks
rm -Rf other_stuff

# move develop to master
git checkout develop
git branch -D master
git branch master
git checkout master
git branch -D develop

# now we're left with master only, make sure no unwanted tags are carried over:
git tag

# for each unwanted tag do:
git tag -d tagname

git remote remove origin
git remote add git@.../NEWREPO.git
git clone --bare . NEWREPO.git
cd NEWREPO.git

# create new repo on github and push:
git push --mirror git@github.com:.../NEWREPO.git

Job done.