mike
mike

Reputation: 5055

Copy Git repository into subdirectory of existing repository with commit history

I want to copy a (local) Git repository into a subdirectory of another, already existing Git repository while keeping the history intact.

I already tried

cd already-existing-target-repo/
git subtree add -P modules/rocket-module C:/DEV/java/repositories/rocket-module.git/  master

but the command does not import the commit history.

I also thought about trying an approach from here, where the source repository was added as a remote to the target repository, including a subsequen checkout and moving step. But unfortunately there are several files with the same name (i.e. pom.xml).

How can I keep the commit history?

Upvotes: 6

Views: 3094

Answers (1)

Joe McMahon
Joe McMahon

Reputation: 3392

Handwaving the details significantly, I've done this process successfully using git-filter-branch to move all the commit paths in the repo being imported to their new path, and then git merge --allow-unrelated-histories to merge the commits in the new subdirectory into the master repo's commit history.

If you've got source changes (like include paths) to update as well, you'll have to filter the branch in the repo being imported, transform the include paths (I used a couple of small Perl scripts to walk the trees and make the changes), commit the changes in the old repo, and then do the merge into the primary repo.

This Stack Overflow post summarizes the process and provides some scripts to do it: https://stackoverflow.com/a/43345686/39791 - there are other methods on the page that may work as well; I have used this one successfully for a number of repos.

Upvotes: 2

Related Questions