111

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?

2
  • 2
    Added git-submodules tag since this is very useful for converting part of a repo into a submodule. Commented Aug 1, 2012 at 18:53
  • Possible duplicate of Export subtree in Git with history Commented Nov 27, 2016 at 18:29

3 Answers 3

100

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.

Sign up to request clarification or add additional context in comments.

11 Comments

What if I wanted to exclude foodir, and remove all it's history?
Note: if you want several directories, You will need to specify --subdirectory-filter multiple times. E.G. git filter-branch --subdirectory-filter foodir --subdirectory-filter bardir, etc. --subdirectory won't take multiple dirs, but can be specified multiple times.
@Adam If you want to keep the history of foodir in the original project, not rewriting its history, just git rm -r foodir should be sufficient (that will also delete the copy in your working tree; if you don't want that, use --cached). If you wanted to remove it entirely from the history (answering @ilius's question too), you want something like git filter-branch --index-filter 'git rm -r --cached --ignore-unmatched foodir' -- --all
@ilius Sorry I missed your question earlier, see my reply to above for an answer for how to remove a directory and its history.
@ilius Yep, that works too. For a large project, the --index-filter solution is faster than the --tree-filter, as it doesn't have to actually check out the files, it can just manipulate the index directly. The --tree-filter can be a little easier to use however, as you can use ordinary filsystem operations rather than having to work with index manipulation operations.
|
4

To export a folder as a new repository you need:

  1. To clone the repository where the folder you want to export is.
  2. To create a empty repository on your hosting provider as GitHub, to store the exported folder.
  3. Open the cloned repository folder and run this command:

    git subtree push --prefix=YourFolderNameToExport https://github.com/YourUserName/YourNewCleanRepoName master 

1 Comment

git subtree is not available as Cygwin package. If you need it: stackoverflow.com/a/27116828/2484903
2

The point of git is that the history is embodied in each commit by hashing the parent commit. You could "replay" the commits (this is essentially how the svn-importer works) into a new repository and only keeping each sub-project. This, however, would destroy the meaning of the commit hashes. If you have no problem with that then so be it.

In the past I've just cloned it and moved on. This makes things larger but disk space is cheap; my time is expensive.

I also don't know of any tools to splice out a directory. I suppose you could git-log on the directory to find all commits on it then replay the commits with something like git-fast-export?

1 Comment

I upvoted because this because didn't deserve to be negative -- I certainly wouldn't follow this approach but can see he was trying

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.