Skip to content

Instantly share code, notes, and snippets.

@rkumar
Created November 18, 2011 09:30
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 rkumar/1376001 to your computer and use it in GitHub Desktop.
Save rkumar/1376001 to your computer and use it in GitHub Desktop.
detach subdir from one repo to another
## vim:tw=72:ai:formatoptions=tcqln:nocindent
## This is taken from stack overflow. article titled "detach subdir"
# http://stackoverflow.com/questions/359424/detach-subdirectory-into-separate-git-repository
# However, it was all scattered, and no clue where to run what command.
# change REPO for each subdir. Do not run this as a script, preferably cut
# paste each line and run it.
exoprt REPO=core
# come out of rbcurse to higher dir
# cd ..
# rbcurse is the existing repo
# rbcurse-core is the new repo i wish to create which contains only
# lib/rbcurse/core from the old rbcurse
git clone --no-hardlinks rbcurse rbcurse-$REPO
cd rbcurse-$REPO
git remote rm origin
# this removed everything other than lib/rbcurse/core
git filter-branch --subdirectory-filter lib/rbcurse/$REPO HEAD -- --all
# everything was placed in the root folder, so i have to
# move it back into the appropriate folder
mkdir -p lib/rbcurse/$REPO
git mv include lib/rbcurse/$REPO
git mv widgets lib/rbcurse/$REPO
git mv util lib/rbcurse/$REPO
git mv system lib/rbcurse/$REPO
git commit -m "moved files to new subdirectory"
git reset --hard
/bin/rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --aggressive --prune=now
# create a new repo for this, click this link on browser
# http://help.github.com/create-a-repo/
git remote add origin git@github.com:rkumar/rbcurse-$REPO.git
#git push -u origin master
# in my case
# git commit -m "moved files to new subdirectory"
git push -u origin 1.5.0
# until now we have created a new repo, but stuff still exists in the
# old rbcurse repo for $REPO
# go back into the old repo
cd ../rbcurse
# takes a few minutes
git filter-branch -f --index-filter "git rm -q -r -f --cached --ignore-unmatch lib/rbcurse/$REPO" --prune-empty HEAD
# no need to commit. nothing to commit
# won't push, gives an error of fast_forward
#### Had to do a git push -f origin 1.5.0
# finally to update master with 1.5.0 since merge was not allowing me
# go to github admin and rename default to some other branch
git branch -m master master-old # rename master on local
git push -f origin :master # delete master on remote
git push origin master-old # create master-old on remote
git checkout -b master 1.5.0 # create a new local master on top of 1.5
git push origin master # create master on remote
# go back to admin and rename default to master
@rkumar
Copy link
Author

rkumar commented Nov 18, 2011

The last part about master and 1.5.0 was only because i had done all this from a branch. If you've done all this from master tiself, then skip the part about updating master with 1.5 and beyond. Just do a "git push -f" and get on with other directories you may wish to detach.

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