Selectively move files and directories to a new repository (preserving history)

I have a project with a structure like this:

src/
scripts/
db/
other_stuff/
even_more_stuff/
file1
file2
file3

This repository needs to be split up. Folders db and scripts need to be split out into their own repositories, and I know that this can be easily done with git filter-branch --subdirectory-filter ...

I also need to create a new main repository based on the current structure (including history), but excluding anything that is already split to its own repository or any files on a specific list of files to exclude: file1, file3, even_more_stuff. Is there a way to use git filter-branch to filter out files by specific names?

The resulting main repository should simply be:

src/
other_stuff/
file2

One catch to this is I'm not supposed to make any changes to the original repository, so I can't just delete file, file3, etc... and then copy the remainder to a new repository.

Upvotes: 5

Views: 6592

Answers (1)

OK, here's how I did it using git-filter-repo:

  1. Install git-filter-repo
  2. freshly clone the source repository
  3. cd my-source-repo
  4. git filter-repo --path src --path other-stuff --path file2
  5. Check the results, remaining files and git log look OK.
  6. clone the target repository (mine was empty)
  7. cd my-target-repo
  8. Add the source as a remote of the target: git remote add src-repo path/to/my-source-repo
  9. Pull from source repository: git pull src-repo main --allow-unrelated-histories
  10. Clean up remote: git remote rm src-repo
  11. Push to origin: git push --set-upstream origin main

(Change branch names where appropriate.)

I've only done this once, so I don't know if there's a shorter, easier way, but this was still better than using git filter-branch.

Upvotes: 19

Related Questions