There is a point to this story, but it has temporarily escaped my mind...
Contact Me MyFaceBook MyLinkedIn MyGitHub MyTwitter

Move a Sub-Directory Into Separate GIT Repository

I recently had a GIT repository with a sub-directory that should have been it own repository. I also wanted to keep all of the history in the sub-directory. These are the steps that I followed to move that sub-directory into a new separate GIT repository.

Old Structure

project/
  .git/
  p1/
  p2/
  p3/

New Structure

project/
  .git
  p1
  p3

p2/
  .git

Method

$ git clone --no-hardlinks project p2
Cloning into nuget-packages...
done.
$ cd p2
$ git gc
Counting objects: 640, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (225/225), done.
Writing objects: 100% (640/640), done.
Total 640 (delta 401), reused 605 (delta 390)
$ git status
# On branch master
nothing to commit (working directory clean)
$ git filter-branch --subdirectory-filter p2 HEAD -- --all
Rewrite 352c51285e647fc31d37aa42479162160def0594 (31/31)
Ref 'refs/heads/master' was rewritten
$ git reset --hard
HEAD is now at df4f12e removed nant from source control, it is now stored locally on the drive outside of the repository...
$ rm -rf .git/refs/original/
$ git reflog expire --expire=now --all
$ git gc --aggressive --prune=now
Counting objects: 671, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (646/646), done.
Writing objects: 100% (671/671), done.
Total 671 (delta 426), reused 214 (delta 0)
Copyright © 2019 by Julian Easterling. SOME RIGHTS RESERVED.
Privacy Policy              Terms of Use             


Creative Commons License
Except where otherwise noted, content on this site is
licensed under a Creative Common Attribution-Share Alike 4.0 International License.


All of the opinions expressed on this website are those of Julian Easterling and
do not represent the views of any of my current and previous clients or employers in any way.

If you notice an error on the site or content that has not been properly attributed, bring
it to my attention using the contact page and I will endeavor to fix it as soon as I can.

I accept no responsibility or liability for any damages incurred by following any of
my advice or by using any of the information on my site or of those sites that I link to.