0

I have a git project that is currently growing and several parts are starting to become projects in their own right.

So, I want to create submodules for them, but preserve the history of the files. There were moves between the different folders (eG from partThatIsNotItsOwnProject to partThatIsItsOwnProject and I'd like to have the history for that move too.)

Before:

/.git
/someFiles
/partThatIsItsOwnProject
--/subdirectory
/partThatIsNotItsOwnProject
--/subdirectory

I'd like to have:

/.git
/someFiles
/partThatIsItsOwnProject
--/.git
--/subdirectory
/partThatIsNotItsOwnProject
--/subdirectory

So, I guessed git subtree would do that, but it seems to keep the original git repository and only display a part of it. git submodule seems to be the target of this setup, but how do I do the transition?

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72

2 Answers2

1

To do so you'll need to rewrite history of whole repository to exclude everything that is not related to directory you want to put in a separate repo. The git filter-branch command is useful for that. You can find an example of script that does that at [0]. Note that you might need to adjust it to your needs.

In general, git filter-branch [1] allows you to rewrite git history by applying your filters to every commit in the repo sequentially. Note that although using --tree-filter is the simplest way to achieve what you need, it is extremely slow as it does proper checkout of every commit along the way, so editing index directly with --index-filter is better.

[0] https://github.com/openstack/oslo.tools/blob/master/filter_git_history.sh

[1] https://git-scm.com/docs/git-filter-branch

Yorik.sar
  • 5,269
  • 2
  • 20
  • 20
1

Fork you main git, then rename the forked into "partThatIsItsOwnProject", and then remove everything that is not part of that "subdir". Then add the forked git as a submodule to your main git, all history is preserved.

Repeat for all subdirs that you would like to breakout from the master git.

Johan
  • 20,067
  • 28
  • 92
  • 110