Skip to content

Instantly share code, notes, and snippets.

@rdoursenaud
Created October 12, 2016 11:11
Show Gist options
  • Save rdoursenaud/e4de5011a55a117c130299c77dee41f9 to your computer and use it in GitHub Desktop.
Save rdoursenaud/e4de5011a55a117c130299c77dee41f9 to your computer and use it in GitHub Desktop.
Extract a directory based GIT repository to separate repositories keeping all branches and history
#!/bin/sh
# Copyright (C) 2016 Raphaël Doursenaud
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
REPO_URL=''
REPO_NAME=''
# If your directories are at the repository root. Edit the code to remove SUBDIRECTORY references!
SUBDIRECTORY=''
## Clone repo
git clone ${REPO_URL}
cd ${REPO_NAME}
# Update all branches
for branch in $(git for-each-ref refs/remotes/origin --format='%(refname:strip=3)')
do
echo "Updating branch ${branch}"
git checkout ${branch}
done
cd ..
# For each directory
for dir in $(ls -d ${REPO_NAME}/${SUBDIRECTORY}/*/)
do
module=$(basename ${dir})
echo "Extracting module: ${module}"
# Clone to a separate repository
git clone ${REPO_NAME} ${module}
cd ${module}
# Filter each branches
for branch in $(git for-each-ref refs/remotes/origin --format='%(refname:strip=3)')
do
echo "Extracting branch: ${branch}"
git checkout ${branch}
git filter-branch -f --prune-empty --subdirectory-filter ${SUBDIRECTORY}/${module}
done
cd ..
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment