Skip to content

Instantly share code, notes, and snippets.

@vi
Created December 24, 2010 02:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vi/753824 to your computer and use it in GitHub Desktop.
Save vi/753824 to your computer and use it in GitHub Desktop.
Script to extract submodules from git repositories. Depends on --split-submodule filter.
#!/bin/bash
# Extract subdirectory into a submodule.
if [ -z "$1" ]; then
echo "Usage: extract-submodule subdirectory"
echo "No trailing slash"
echo "Should be run from the root directory of repository"
echo "Everything should be clean prior to running this script"
echo "You need to manually copy submodule somewhere and update .gitmodules file appropriately"
exit 1;
fi
path="$1"
set -ex
git filter-branch --split-submodule "$path" --tag-name-filter cat -- --branches --tags
git clone . "$path"
pushd .
cd "$path"
set +e
rm -Rf .git/refs/original
set +x
for i in `git show-ref | cut -b 42-`; do
set -x
git update-ref -m 'submodule conversion' $i $i:$path || git update-ref -d $i
set +x
done
set -x
git reset --hard
popd
cat >> .gitmodules <<EOF
[submodule "$path"]
path = $path
url = please://fix/me/
EOF
set +x
echo "You should probably do 'git submodule update --init' now"
@vi
Copy link
Author

vi commented Dec 25, 2010

The --split-submodule patch for git is mirrored at http://vi-server.org/vi/bin/git-extract-submodule.patch

The script itself is mirrored at http://vi-server.org/vi/bin/git-extract-submodule

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