Kerrick
Kerrick

Reputation: 7478

How do you split a git repo, mainaining only the history per directory?

Say I have a git repository with the structure:

my-repo/
  .git/
  directory-one/
    [...]
  directory-two/
    [...]

I need to split the repo into two repositories (one for directory-one and the other for directory-two). I'd like to maintain the history of each of those subdirectories, but no code from directory-one can appear in the history of directory-two, and vice versa.

Is this possible?

Upvotes: 1

Views: 152

Answers (2)

Kerrick
Kerrick

Reputation: 7478

Note: You lose any non-merged, non-master branches with this process.

  1. cp -R my-repo/ my-repo-one/
  2. cd my-repo-one/
  3. git filter-branch --tree-filter "rm -rf directory-two" HEAD
  4. mv .git directory-one/
  5. git checkout -f
  6. git filter-branch --subdirectory-filter directory-one

This gets you a repo with only the history from directory-one/. Repeat the process for directory-two/.

Thanks to minitech for steps 4-6!

Upvotes: 0

Ry-
Ry-

Reputation: 224855

You can use git filter-branch with --subdirectory-filter, which was built exactly for this:

# Clear the two directories entirely
rm -rf directory-one/ directory-two/
mkdir directory-one/ directory-two/

# Add the original .git to each one
cp -R .git directory-one/
mv .git directory-two/

# Check out the repo into the empty directory and filter it to that directory
cd directory-one/
git checkout -f
git filter-branch --subdirectory-filter directory-one/

# Repeat
cd ../directory-two/
git checkout -f
git filter-branch --subdirectory-filter directory-two/

Make a backup first.

Upvotes: 2

Related Questions