Skip to content

Instantly share code, notes, and snippets.

@edouard-lopez
Created November 2, 2015 15:04
Show Gist options
  • Save edouard-lopez/62772dd17d15047ae209 to your computer and use it in GitHub Desktop.
Save edouard-lopez/62772dd17d15047ae209 to your computer and use it in GitHub Desktop.
Detach subdirectory into separate Git repository and preserve history (see http://stackoverflow.com/q/359424/802365)
#!/usr/bin/env bash
# DESCRIPTION:
# Detach subdirectory into separate Git repository and preserve history (see http://stackoverflow.com/q/359424/802365)
#
# USAGE:
# bash -x ./extract-to-submodule.bash password-strength password-toggle phone-ribbon
if [[ $# -lt 2 ]]; then
echo "missing parent directory or subdirectories list."
exit;
else
fat_repo="$(pwd)/$1"; shift 1
ancestor_dir="$fat_repo/.."
subdirs=( "$@" )
echo "Extracting ${subdirs[@]}."
fi
for subdir in "${subdirs[@]}"; do
cd "$fat_repo"/
module="ui-$subdir"
module_remote="ssh://[email protected]:33622/akema-lab/$module.git"
# isolate to branch
git subtree split --prefix="$subdir" --branch="$subdir"
# extract to new directory
mkdir "$ancestor_dir/$module" \
&& cd "$ancestor_dir/$module" \
&& git init \
&& git pull "$fat_repo" "$subdir" \
&& git remote add origin "$module_remote" \
&& git push origin -u master
# clean fat repo
cd "$fat_repo"/
git rm -rf "$subdir"
git submodule add "$module_remote" "$module"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment