How to merge N git repositories into one preserving their histories?
I have a project, which consists of several separate Git repositories (merge several Maven projects each with a separate Git repository).
Now I want to create one Git repository and put the contents of each of those repositories into sub-directories of it, preserving their respective histories (merge several Maven projects into one multi-module Maven project).
How can I do that?
I tried to apply the answer to a similar SO question:
- Let's call individual repositories P1-PN and the unified repository U.
- Create repository U.
- Create branch P1 in U (git checkout -b P1).
- In P1 directory, run the command git remote add U firstname.lastname@example.org:/U.git.
- In P1 directory, git push U P1.
When I do this, I get following error in the last step:
error: src refspec ccplogic does not match any. error: failed to push some refs to 'email@example.com:/U.git'
One way of doing this is by adding each repository as a git submodule, but it sounds as if you want a single repository instead.
You can make a single repository with the history of all the other repositories by using a script called git-subtree, which is now part of mainline git, albeit in the contrib directory. To install the git-subtree script, save it to the name git-subtree somewhere on your PATH. (If that worked, then git subtree should give you a usage message.)
Assuming you created a new empty repository with something like:
mkdir example cd example git init touch .gitignore git add .gitignore git commit -m 'Initial commit'
Then for each other repository, you can do something like the following (assuming that the repository is at the URL git://wherever/whatever.git and you want its master branch to appear in the subdirectory whevs):
git remote add whevs-remote git://wherever/whatever.git git fetch whevs-remote git subtree add --prefix=whevs whevs-remote/master
Then do the same for each other repository.