Created
November 10, 2014 13:16
-
-
Save larsmans/caa4a9519bfd481da9f9 to your computer and use it in GitHub Desktop.
Detach subdirectory from Git repository as separate repo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was previously doing this stuff manually. Now I accidentally stumbled upon your gist.
It worked flawlessly! Brilliant!
Thank you!