3

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:

  1. Let's call individual repositories P1-PN and the unified repository U.
  2. Create repository U.
  3. Create branch P1 in U (git checkout -b P1).
  4. In P1 directory, run the command git remote add U git@myuser.beanstalkapp.com:/U.git.
  5. 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 'git@myuser.beanstalkapp.com:/U.git'
Community
  • 1
  • 1
Glory to Russia
  • 17,289
  • 56
  • 182
  • 325
  • That's not the same problem in that question: you want to merge the trees. Pulling all the objects into one repository is the correct thing to do, yes, but then you need to stitch the trees together and save that as a commit. I'm not sure there's a porcelain way to do that, and the plumbing way is pretty nasty IIRC. The push step at least is wrong for you: there's nothing to push to. – Rup May 01 '13 at 14:13

1 Answers1

2

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.

Mark Longair
  • 446,582
  • 72
  • 411
  • 327