Selectively move files & directories to new repo (preserving history)

Viewed 1090

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

1 Answers

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

  1. Install git-filter-repo ( https://github.com/newren/git-filter-repo )
  2. freshly clone source repo
  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.

Related