Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I move a single directory from a git repository to a new repository whilst maintaining the history?

I have inherited a git repository containing multiple projects in separate directories. I'd like to split the repository into new individual repositories, one for each project and then have the master repository contain the projects as submodules. I'd like to do all this whilst maintaining the revision history of the individual projects if possible.

I could clone the repository for each project and remove all the other projects each time, but it there a better way to avoid having the cloned history in each new project repository?

like image 388
cidered Avatar asked May 01 '09 13:05

cidered


People also ask

How do I copy a directory from one github repository to another?

Merge the files into the new repository B. Step 2: Go to that directory. Step 3: Create a remote connection to repository A as a branch in repository B. Step 4: Pull files and history from this branch (containing only the directory you want to move) into repository B.

How do I move a directory in git?

The 'git mv' command is used for moving or renaming a file or directory. When you provide <destination> as the same type (file or directory), it renames the <source> file/directory (which must exist) to <destination>. file/directory.


1 Answers

You can use git filter-branch to rewrite the history of a project. From the documentation:

To rewrite the repository to look as if foodir/ had been its project root, and discard all other history:

git filter-branch --subdirectory-filter foodir -- --all 

Make several copies of your repo, do that for each subdirectory you want to split out, and you should wind up with what you're looking for.

like image 154
Brian Campbell Avatar answered Sep 23 '22 02:09

Brian Campbell