0

Lets say we have Repo-A and Repo-B. I have a folder structure in Repo-A as follows:

Repo-A /Folder-1 
       /Folder-2
       /Folder-3

I want to migrate Folder-1 and Folder-2 to Repo-B. I understand I can migrate a single directory using the method given here - How to move files from one git repo to another (not a clone), preserving history So I do that with Folder-1, however when I do the same thing again to migrate the second folder I get duplicate commits, some commit which has affected both Folder-1 and Folder-2 both in the past. How can I solve this?

I also tried putting all the files I want to migrate in one particular folder, but I lose my commit history when I migrate that folder.

user3481478
  • 387
  • 1
  • 3
  • 15

1 Answers1

1

It sounds like you want a single history containing all changes to files in the two folders. You can't do that with --directory-filter, but you can still use filter-branch with the --index-filter.

git filter-branch --index-filter 'git rm --cached -r --ignore-unmatch -- :/:* :/!:Folder-1 :/!:Folder-2` --prune-empty -- --all

Using git rm as an index-filter creates a new commit from each original commit by loading the original commit to the index, removing some files, and committing the new index.

Each of the :/!:... options tells git rm not to delete the named folder and its contents, and :/:* tells it to delete everything else.

The --prune-empty option eliminates from the new history any commit that only changed files outside the folders you keep - because those would look like they didn't do anything in the new history. You can omit this option if you want to keep those commits anyway.

The above keeps the files in their original directories. If you want something different, then you may need to use a --tree-filter instead of --index-filter. That option is considerably slower (especially if the repo is large), but it's easier to make more complex changes (because the filter script just has to operate on files on disk, rather than directly transform the index).

That said, as of sometime recently the documentation recommends against using filter-branch. I'm not yet familiar with the tool they suggest using instead.

So, here's the filter-branch docs, from which you can either follow the warning and look up a different tool, or learn more about the above procedure: https://git-scm.com/docs/git-filter-branch

Mark Adelsberger
  • 32,904
  • 2
  • 24
  • 41