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:

  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'

Answers


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.


Need Your Help

Can't find ViewByID in Custom Adapter

android android-adapter

I created my own custom adapter that extends Array Adapter:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.