Search code examples
gitgithubconfiguration-management

Git: Combining two independent repos to one without losing the history


I got two repos with the following folder structure:

> folder1
  >.git
  > file1
  > file2

> folder2
  >.git
  > file3
  > file4

Now I want to create a new repo combining the two formerly independent root folders under one common root:

> repo
   >.git
   > folder1
      > file1
      > file2
    > folder1
      > file3
      > file4

How would I do that in Git without losing the history of either formerly independent repository?


Solution

  • I guess something as follows should work:

    1. Adapt the repo folder1 to have the right internal structure.

      cd folder1
      mkdir folder1
      git mv file1 folder1/file1
      git mv file2 folder1/file2
      git commit -a
      
    2. And ditto for the repo folder2.

      cd folder2
      mkdir folder2
      git mv file3 folder2/file3
      git mv file4 folder2/file4
      git commit -a
      
    3. Pull one into the other one with git pull

      cd folder1  # the top one, not the one we have created in the 1st step.
      git pull /path/to/folder2
      

      Or, alternatively, merge both into a newly created repo.

      mkdir repo
      cd repo
      git init
      git pull /path/to/folder1
      git pull /path/to/folder2