This simple script allows to split a folder from a project (make a backup first!), and will keep all branches and valid tags that use this folder.
#!/bin/bash URL=$1 FOLDER=$2 SPLITTER=$3 set -e function git_extract(){ pushd $1 git remote rm origin echo "Extracting $2 from $1" echo "Original size $(du -h -s .)" for TAG in $( git tag -l ); do if [ -z "$(git ls-tree refs/tags/$TAG $2)" ]; then echo "$TAG will be deleted" git tag -d $TAG fi; done for BRANCH in $( git branch -a | grep -v master | grep -v branches ); do if [ -z "$(git ls-tree refs/heads/$BRANCH $2)" ]; then echo "$BRANCH will be deleted" git branch -D $BRANCH fi; done git filter-branch \ --subdirectory-filter $2 \ --tag-name-filter cat \ --prune-empty -- --all rm -rf refs/original if [ "$3" == "mark-split" ]; then git filter-branch --msg-filter ' cat &&; echo "" echo "Splitted-from: $4" echo "Splitted-by: $5" ' --tag-name-filter cat -- --all fi rm -rf refs/original logs lost-found git reflog expire --all git gc --prune=0 --aggressive # shrink the project please! echo "new size $(du -h -s .)" popd } git clone --mirror --bare $URL split-base-mirror git_extract split-base-mirror $FOLDER mark-split $URL $SPLITTER
The script takes 3 arguments: url from which we get the repo, folder we’re splitting and your Name for tagging it.