Skip to content

Instantly share code, notes, and snippets.

@jbeales
Last active October 17, 2018 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbeales/bb54bf1e91b78b837cd3f49657ca4d41 to your computer and use it in GitHub Desktop.
Save jbeales/bb54bf1e91b78b837cd3f49657ca4d41 to your computer and use it in GitHub Desktop.
A bash script to separate directories from within a large git repository into their own repository, maintaining relevant history
#!/bin/bash
# Enter the paths in the main repo to the subdirectories you want to extract
# Separate paths with spaces or newlines
repos=(
artwork
checklists
code/comingsoon
code/utility
)
echo "Total repos to export : ${#repos[*]}"
# Put the directory of the main repo here.
master=monolith-repo-master
for repo in "${repos[@]}"
do
subdir=$(basename $repo)
echo "${repo} => ${subdir}"
cp -R $master $subdir
cd $subdir
git filter-branch --prune-empty --subdirectory-filter $repo master
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now
cd ../
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment