简体   繁体   中英

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'

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM