Skip to content

Instantly share code, notes, and snippets.

@esebastian
Created July 5, 2014 18:18
Show Gist options
  • Save esebastian/b49cb0d5e5208daa896c to your computer and use it in GitHub Desktop.
Save esebastian/b49cb0d5e5208daa896c to your computer and use it in GitHub Desktop.
Move the current git working tree into a subfolder
#!/bin/sh
# Move the current working tree into a subfolder.
#
# This will rewite all branches and tags, so this should not
# be performed on a repo that has been shared with others, because it
# will force them to reset their history.
#
# Use at your own risk.
#
# Put the script in your path and invoke it this way:
# git subdir <NAME>
if [[ ! -n $1 ]]; then
echo "usage: git subdir <NAME>\n"
exit 1
fi
echo "Moving working tree into $1..."
PREFIX=$1
git filter-branch --tag-name-filter cat --index-filter 'SHA=$(git write-tree); rm $GIT_INDEX_FILE && git read-tree --prefix=$PREFIX/ $SHA' -- --all
unset PREFIX
[[ $? -eq 0 ]] && echo Done!
[[ $? -ne 0 ]] && echo Error!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment