When developing, often there are parts of the project that are worth releasing as separate independent projects. There is a tool from the Symfony authors - https://github.com/splitsh/lite - which can assist, but it took a little working out.

The script I've ended up using is below. I run it from the root of the project.

It should be usable by simply adjusting the few variables are the top and then run the script with bash.

Running through the commands manually the first time or two is recommended/a good idea so its understood what the script is doing, but once comfortable, it can be run quickly and easily to keep a sub-project up to date when working mostly on a much larger mono repo style project.

Obviously, be careful with something like this that will push to a repository.


# This script automates the creation and updating of a subtree split
# in a second repository.
set -eu

command -v splitsh-lite >/dev/null 2>&1 || { echo "$0 requires splitsh-lite but it's not installed.  Aborting." >&2; exit 1; }

# Adjust for your repositories.


# The rest shouldn't need changing.
temp_repo=$(mktemp -d)
temp_branch=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${1:-8} | head -n 1)

# Checkout the old repository, make it safe and checkout a temp branch
git clone ${source_repository} ${temp_repo}
cd ${temp_repo}
git checkout ${source_branch}
git remote remove origin
git checkout -b ${temp_branch}

# Create the split, check it out and then push the temp branch up
sha1=$(splitsh-lite --prefix=${source_dir} --quiet)
git reset --hard ${sha1}
git remote add remote ${destination_repository}
git push -u remote ${temp_branch}:${destination_branch}

# Cleanup
cd /tmp
rm -rf ${temp_repo}