Skip to content

Instantly share code, notes, and snippets.

@TechNickAI
Created September 18, 2012 02:22
Show Gist options
  • Save TechNickAI/3740910 to your computer and use it in GitHub Desktop.
Save TechNickAI/3740910 to your computer and use it in GitHub Desktop.
move subdirectory of repo to a new repo
#!/bin/bash
# abort on any errors
set -e
# be verbose
set -x
# Move a subdir to a new repo
# http://stackoverflow.com/questions/359424/detach-subdirectory-into-separate-git-repository
oldgiturl="git@git.assembla.com:krux_interchange.3.git"
sourcerepo="krux_interchange.3"
sourcesubdir=webapp/krux
newgiturl="git@github.com:krux/dataconsole.git"
destrepo=`basename $newgiturl | sed 's/\.git//'`
if [ -d "$sourcerepo" ] ; then
echo "$sourcerepo exists"
exit
fi
if [ -d "$destrepo" ] ; then
echo "$destrepo exists"
exit
fi
git clone $oldgiturl
# Copy the directory.
# Don't use 'git clone' because we don't know what the upstream branches are
# Don't use 'cp -ra' because it doesn't get symlinks
rsync -a $sourcerepo/ $destrepo
# Pull over the existing branches
cd $destrepo
for branch in `git branch -a | grep remotes | grep -v " -> " | grep -v master | sed 's/remotes\/origin\///'`; do
git branch -t $branch origin/$branch
done
# Commit the added branches
git commit || true
# Filter what we don't want
git filter-branch --tag-name-filter cat --prune-empty --subdirectory-filter $sourcesubdir -- --all
# Pull over the old .gitignore
cp ../$sourcerepo/.gitignore ./
git add .gitignore
git commit -m "Pull over .gitignore"
# Clean up
git gc
# Set the remote
git remote set-url origin $newgiturl
# Push to master
git push -u origin master
# Push the other branches
for branch in `git branch | grep -v master`; do
git push origin $branch
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment