Maybe there is already a solution out there, but other questions/answers seem to address slightly different issues (or I don't understand them really).
My intention is to detach a subdirectory of a Git repository and make it an independent repository, while keeping the history intact, but only the history of the subdirectory. This question first seemed to do the trick, but then I noticed a flaw in it:
git filter-branch --subdirectory-filter
only preserves commits that relate to the given subdirectory. But this means that commits are removed that affect files that are in that subdir now but have been moved there from other locations.
I noticed this because the first commit of my 'cleaned up' repository was 'Move everything to subdir X'. This means that my files had been at another location before, but the commits from that time weren't preserved.
So what I'd need is a command (or sequence of commands) that:
- removes all commits in the repository
- except commits that contain files that
- are in the given subdirectory now or
- are prior versions of these files in other locations.
B)
Possibly some of these commits also contain files that don't match these conditions. If these files could be pruned completely from the repository that would be a nice add-on.
Edit:
The solution linked above pulls the subdir content in the new repository to the root directory of the repo. As @Amber pointed out this would cause trouble with files that already had lived in the root dir. So what I would like to achieve is:
Original dir structure:
\Old-Repo
\.git
\ABC
|- dir content
\DEF
|- dir content
\GHI
|- dir content
The dir structure of the detached repository should be:
\New-Repo-DEF
\.git
\DEF
|- dir content
and not:
\New-Repo-DEF
\.git
content of old DEF subdirectory
Then I would afterwards move the content from the DEF subdir to the root dir with a regular commit.