I have a git repository projectA
with directories like this:
projectA
/dir1
/dir2
/dir3
/dir4
dir1, dir2, dir3 and dir4 all contain several files and deeper subdirectories.
I want to move dir1 and dir2 with all their contents into a new git repository projectB
and preserve the commit history that are relevant for these files. By using gitk --follow dir1/somefile
I am able to trace the history of a file even if it has been renamed/moved.
projectA
/dir3
/dir4
projectB
/dir1
/dir2
I first tried collecting dir1 and dir2 into a single directory and use filter-branch like this to get a new repository with only these files. I end up with the correct files, however the only history that is preserved is the final commit that collected dir1 and dir2. So it seems like this method will not preserve any commits that are connected to previous names/paths these files had.
I tried using a different filter-branch approach:
git filter-branch --index-filter 'git rm --cached -qr --ignore-unmatch -- . && git reset -q $GIT_COMMIT -- dir1 dir2' --prune-empty -- --all
This yielded the same result as the first one.
I also tried using patching like described here and here, however this also failed to preserve movement/naming history.
I tried using subtree as explained here. The plan is to extract a directory as a new branch and then using that new branch to create projectB repo:
git subtree split --prefix=dir1 -b projectb
Again I ended up with a new branch that contained only the commits from the last name/path of each file.
Just to be clear, apart from collecting dir1 and dir2 into a single directory, the files within these directories have been renamed/moved several times before and this history needs to be preserved.