2

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
  1. 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.

  2. 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.

  3. I also tried using patching like described here and here, however this also failed to preserve movement/naming history.

  4. 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.

EJS
  • 1,011
  • 5
  • 14
  • 28
  • Maybe you should turn the directories to move (dir1 and dir2) into git projects with [subtree](https://github.com/apenwarr/git-subtree/blob/master/git-subtree.txt#L101), then add them as such into projectB with [git subtree add](https://github.com/apenwarr/git-subtree/blob/master/git-subtree.txt#L68) – smarber Jan 05 '18 at 16:17
  • The only history in a repository is the set of commits in the repository. Any *file* history you get (e.g., that from `--follow`) is *synthesized* by looking at the commits (the real history) and using `git diff` or similar to make guesses about name-changes. That will continue to be the case no matter what you do: if you have enough commits so that the synthetic history matches, it matches. If you don't, it doesn't. So the problem reduces to carrying across all commits that matter. – torek Jan 05 '18 at 17:54
  • @smarber Thank you for the suggestion. I tried it but couldn't make it work. See the newly added point 4 in my question. – EJS Jan 08 '18 at 16:11
  • As mentioned by @torek, would guess preserving the move history is the tricky part. Perhaps try git split suggested in this answer? https://stackoverflow.com/questions/43519572/copy-subdirectories-from-git-repo-into-a-new-repository-with-a-new-directory-st – Max MacLeod Jun 26 '23 at 10:59

0 Answers0