#!/bin/sh
# Usage: git-detach <directory> <target>
# Creates a new Git repository at <target> from the contents of <directory>
# and its history. Does not remove the directory from its original repo.
#
# E.g.: suppose project/ is a Git repository with a subdirectory lib/, then
# git-detach project/lib/ standalone-lib/
# creates a new repository standalone-lib/ holding the contents of project/lib/
# as a separate repo.
if [ $# != "2" ]; then
echo "Usage: git-detach <directory> <target>" >&2
exit 1
fi
set -e #-x
src="$1"
tgt="$2"
subdir=$(cd "$src" && git rev-parse --show-prefix)
git clone $(cd "$src" && git rev-parse --show-toplevel) "$tgt"
cd "$tgt"
git filter-branch --subdirectory-filter "$subdir" --prune-empty