-1

I am looking for advice or warnings about merging three Git repositories. I need to maintain the commit history and all branches.

I have examined this question and this question, and it seems like the "git-subtree" command would have done what I want, but it seems like that command has been removed or something (the comments seem to conflict on this issue.) There is a "git subtree" command but I am not sure what it does, and the documentation is rather thin.

I have three Git repositories that each contain a Maven project. The files and repositories do not overlap at all (no project is a Git submodule of any other, for example.) They are totally separate except for code in B and C that references A:

ProjectA.git
    pom.xml
    /src/....

ProjectB.git
    pom.xml
    /src/...

ProjectC.git
    pom.xml
    /src/...

I would like to merge them into one "Parent" repository:

Parent.git
    /ProjectA
        pom.xml
        /src/...
    /ProjectB
        pom.xml
        /src/...
    /ProjectC
        pom.xml
        /src/...

I need to maintain the commit history and all branches, but I am not interested in maintaining the individual repositories as submodules or as separate repos. In fact, it would be better if they went away after this process so that people are not tempted to continue using them.

What is the simplest and most reliable way of doing this? Is there a way of accessing the git-subtree command (I am running on Windows 7 using Git 1.8.0) or a way of doing this merge without that command?

Community
  • 1
  • 1
user1071914
  • 3,295
  • 11
  • 50
  • 76

2 Answers2

2

I think git subtree may be overkill for this use case, because you want to just irreversibly combine the repositories.

First, in each existing repository, create the relevant project subdirectory (projectA or whatever) and move the existing files and directories into it using git mv. Commit those changes. Then check out each other branch in turn and repeat that process for each branch.

Then use git remote add, git fetch and then git merge for each pair of same-named branches in each pair of repositories. If you have any uniquely-named branches in any of the repositories, you'll need to merge them with master (using the -s ours or -s theirs merge strategy depending on whether you are merging FROM or TO them). Otherwise, they'll be very odd because they will have 2/3rds of the project missing (but possibly that doesn't really matter, since the other 2/3rds will still be there in the other branches).

Since there will now be no conflicts, this should just work, and preserve all your history. But I haven't tried this, so don't delete your old repositories for a while, just in case it goes wrong!

Robin Green
  • 32,079
  • 16
  • 104
  • 187
0

What are you talking about? You run git-subtree by typing git subtree, as explained in your first question's example code.

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • Yes, the git subcommands are labelled as `git-commandname` in the man pages because man page names aren't allowed to have spaces in, that's all. However, I think the `git subtree` command may be overkill for this use case. – Robin Green Oct 26 '13 at 21:25
  • The comments on the first question variously say that "git-subtree" was (1) discontinued (2) was included in Git and (3) is discontinued but still not included in Git. I really didn't know which one to believe, if any of them. Also, there are at least three wildly different "answers" to that question. Which one of those should I have chosen? Bear in mind that damaging your company's source code repository could get you fired, which is why I'm being so careful. :-D – user1071914 Oct 26 '13 at 21:33
  • @user1071914 regarding last point, obviously would make backups - since this is such a deep operation, in this case, `git clone`s offline - and verify carefully. – djechlin Oct 26 '13 at 21:55