views:

1708

answers:

3

Consider the following scenario: I have developed small experimental project A in its own git repo. It has now matured, and I'd like A to be part of larger project B, which has its own big repository. I'd now like to add A as a subdirectory of B.

How do I merge A into B, without losing history on any side?

+2  A: 

You got to project B's directory, create a directory for project A, checkout the project A to that directory and define project A as project's B git submodule.

Eimantas
+6  A: 

The submodule approach is good if you want to maintain the project separately. However, if you really want to merge both projects into the same repository, then you have a bit more work to do.

The first thing would be to use git filter-branch to rewrite the names of everything in the second repository to be in the subdirectory where you would like them to end up. So instead of foo.c, bar.html, you would have projb/foo.c and projb/bar.html.

Then, you should be able to do something like the following:

git remote add projb [wherever]
git pull projb

The git pull will do a git fetch followed by a git merge. There should be no conflicts, if the repository you're pulling to does not yet have a projb/ directory.

Further searching indicates that something similar was done to merge gitk into git. Junio C Hamano writes about it here: http://www.mail-archive.com/[email protected]/msg03395.html

Greg Hewgill
Thanks a lot, that's exactly what I wanted to do.
static_rtti
Not having tried this myself, the `git pull` step may complain about the inability to find a common ancestor. Some other details may need to be ironed out too.
Greg Hewgill
subtree merge would be better solution, and do not require rewriting history of included project
Jakub Narębski
faving the question and the answer!
Eimantas
+12  A: 
Jakub Narębski