Skip to content

Instantly share code, notes, and snippets.

@larsmans
Created November 10, 2014 13:16
Show Gist options
  • Save larsmans/caa4a9519bfd481da9f9 to your computer and use it in GitHub Desktop.
Save larsmans/caa4a9519bfd481da9f9 to your computer and use it in GitHub Desktop.
Detach subdirectory from Git repository as separate repo
#!/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
@xZero707
Copy link

xZero707 commented Nov 1, 2017

I was previously doing this stuff manually. Now I accidentally stumbled upon your gist.
It worked flawlessly! Brilliant!
Thank you!

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