Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (29.3k points)

I have a Git repository in a folder called XX, and I have a second Git repository called YY.

I want to import the XX repository into the YY repository as a subdirectory named ZZ and add all XX's change history to YY.

Folder structure before:


 |- .git

 |- (project files)


 |- .git

 |- (project files)

Folder structure after:


 |- .git  <-- This now contains the change history from XX

 |-  ZZ  <-- This was originally XX

      |- (project files)

 |-  (project files)

Can this be done, or must I resort to using sub-modules?

1 Answer

0 votes
by (50.2k points)

For merging multiple repositories to one:

Since git version 1.7.11. We are using a strategy known as subtree merging. 

For this question all you need to do:

git subtree add -P ZZ /path/to/XX.git master


Before git 1.7.11 we use something like this:

git remote add other /path/to/XX

git fetch other

git checkout -b ZZ other/master

mkdir ZZ

git mv stuff ZZ/stuff                      # repeat as necessary for each file/dir

git commit -m "Moved stuff to ZZ"

git checkout master                

git merge ZZ --allow-unrelated-histories   # should add ZZ/ to master

git commit

git remote rm other

git branch -d ZZ                           # to get rid of the extra branch before pushing

git push                                    # if you have a remote.

Thus, you can merge multiple repositories to one repository.

Browse Categories