Git: Conversion of a Subdirectory to a Submodule
I have successfully been able to convert Submodules to Subdirectories using commands such as the methods outlined in these two examples:
- How do I add a git submodule that has no master branch?
- Why branch checkout on Windows is so slow, but on Linux so fast?
- Add subproject as usual folder to repository
- Git Restkit submodule .xcodeproj modified all the time
- can't add git submodule to repository
- Git add submodules to super project from existing repos
The challenge is to successfully achieve this on a directory, preserving its history through each conversion.
These two methods outlined above have worked fine on a trivial experimental repository, but does not handle a more complex repository. e.g, a repo with an arbitrary commit pattern.
The problem exposes itself when executing
git filter-branch --subdirectory-filter <lib-directory> -- --all
when trying to convert a directory (that was previously a submodule) to a submodule. If I understand correctly. This was throwing:
Rewrite c95281d27e4602e9af50146eefcf7c28f5bb4f35 (2/11)a989b207d3757f9803fd50fa2d77908a4dc1330e fatal: failed to unpack tree object c95281d27e4602e9af50146eefcf7c28f5bb4f35:lib/test_submodule Could not initialize the index`
of which there were absolutely no similar results online.
It was figured this was occurring due to the recurring reference to the submodule within the INDEX, whereby when the submodule was converted, the above error would occur.
Is there some way of performing a
filter-branch that will allow the avoidance of these earlier references to the submodule??
Edit: I’ve come back looking at this issue again, and I still haven’t found a way to solve it. Using the Subdir->Submodule method with
git filter-branch works fine for a normal directory; but crashes when it hits a submodule.
The crash that keeps happening at this section within git:
I can’t make much sense of it though.
2 Solutions collect form web for “Git: Conversion of a Subdirectory to a Submodule”
For me it worked after adding a trailing slash to the directory:
$ git filter-branch --subdirectory-filter htdocs/typo3_src -- --all Rewrite cbe03e13da071403a2632263f1760b560398cdd3 (1/12) (0 seconds passed, remaining 0 predicted) 004b20fc15023539484c7f5990b99780f54dc0ac fatal: failed to unpack tree object cbe03e13da071403a2632263f1760b560398cdd3:htdocs/typo3_src Could not initialize the index $ git filter-branch --subdirectory-filter htdocs/typo3_src/ -- --all Rewrite ec6e3c7212f1080fc052c87b1129335ab5bee524 (5/10) (1 seconds passed, remaining 1 predicted) Ref 'refs/heads/master' was rewritten Ref 'refs/remotes/origin/master' was rewritten Ref 'refs/remotes/origin/develop' was rewritten WARNING: Ref 'refs/remotes/origin/master' is unchanged WARNING: Ref 'refs/tags/0.0.1' is unchanged
Instead of using the
rev-list option –all, try filtering by commit hash instead.
git filter-branch --subdirectory-filter <lib-directory> <hash>...
The commit that caused the problem in my case was the one containing the
Subproject commit. Example:
git filter-branch --subdirectory-filter <lib-directory> c95281d27e4...