Reputation: 7478
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
Reputation: 7478
Note: You lose any non-merged, non-master branches with this process.
cp -R my-repo/ my-repo-one/
cd my-repo-one/
git filter-branch --tree-filter "rm -rf directory-two" HEAD
mv .git directory-one/
git checkout -f
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
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