Vijay Patel Vijay Patel - 1 year ago 47
Git Question

How to import existing Git repository into another?

I have a (Windows) Git repository in a folder called XXX, and I have second Git repository called YYY.

I want to import the XXX repository into the YYY repository, add all XXX's change history to YYY, and rename XXX to ZZZ.

Folder structure before:

|- .git
|- ZZZ

Folder structure after:

|- .git <-- This now contains the change history from XXX
|- ZZZ <-- This was originally XXX
|- (other folders)

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

Answer Source

Probably the simplest way would be to pull the XXX stuff into a branch in YYY and then merge it into master:


git remote add other /path/to/XXX
git fetch other
git checkout -b ZZZ other/master
mkdir ZZZ
git mv stuff ZZZ/stuff             # as necessary
git commit -m "Moved stuff to ZZZ"
git checkout master                
git merge ZZZ                      # should add ZZZ/ to master
git commit
git remote rm other
git branch -d ZZZ                  # to get rid of the extra branch before pushing
git push                           # if you have a remote, that is

I actually just tried this with a couple of my repos and it works. Unlike Jörg's answer it won't let you continue to use the other repo, but I don't think you specified that anyway.

Note: Since this was originally written in 2009, git has added the subtree merge mentioned in the answer below. I would probably use that method today, although of course this method does still work.