Consider the following scenario:
I have developed a 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?
A single branch of another repository can be easily placed under a subdirectory retaining its history. For example:
git subtree add --prefix=rails git://github.com/rails/rails.git master
This will appear as a single commit where all files of Rails master branch are added into "rails" directory. However the commit's title contains a reference to the old history tree:
Add 'rails/' from commit
<rev> is a SHA-1 commit hash. You can still see the history, blame some changes.
git log <rev> git blame <rev> -- README.md
Note that you can't see the directory prefix from here since this is an actual old branch left intact. You should treat this like a usual file move commit: you will need an extra jump when reaching it.
# finishes with all files added at once commit git log rails/README.md # then continue from original tree git log <rev> -- README.md
There are more complex solutions like doing this manually or rewriting the history as described in other answers.
The git-subtree command is a part of official git-contrib, some packet managers install it by default (OS X Homebrew). But you might have to install it by yourself in addition to git.
git co v184.108.40.206with
... v1.8.3). — Jun 07, 2013 at 14:31
git log rails/somefilewill not display that file's commits history except the merge commit. As @artfulrobot suggested, check Greg Hewgill's answer. And you might need to use
git filter-branchon the repo you want to include. — Oct 08, 2013 at 12:07
git subtreemay not do what you think! See here for a more complete solution. — Feb 01, 2014 at 08:13