title: Merge git repository in subdirectory
body: <p>I'd like to merge a remote git repository in my working git repository as a subdirectory of it. I'd like the resulting repository to contain the merged history of the two repositories and also that each file of the merged-in repository retain its history as it was in the remote repository. I tried using the subtree strategy as mentioned in <a href="http://www.kernel.org/pub/software/scm/git/docs/howto/using-merge-subtree.html" rel="noreferrer">How to use the subtree merge strategy</a>, but after following that procedure, although the resulting repository contains indeed the merged history of the two repositories, individual files coming from the remote one haven't retained their history (`git log' on any of them just shows a message "Merged branch...").</p>
  
  <p>Also I don't want to use submodules because I do not want the two combined git repositories to be separate anymore.</p>
  
  <p>Is it possible to merge a remote git repository in another one as a subdirectory with individual files coming from the remote repository retaining their history?</p>
  
  <p>Thanks very much for any help.</p>
  
  <p>EDIT:
  I'm currently trying out a solution that uses git filter-branch to rewrite the merged-in repository history. It does seem to work, but I need to test it some more. I'll return to report on my findings.</p>
  
  <p>EDIT 2:
  In hope I make myself more clear I give the exact commands I used with git's subtree strategy, which result in apparent loss of history of the files of the remote repository.
  Let A be the git repo I'm currently working in and B the git repo I'd like to incorporate into A as a subdirectory of it. It did the following:</p>
  
  <pre><code>git remote add -f B &lt;url-of-B&gt;
  git merge -s ours --no-commit B/master
  git read-tree --prefix=subdir/Iwant/to/put/B/in/ -u B/master
  git commit -m "Merge B as subdirectory in subdir/Iwant/to/put/B/in."
  </code></pre>
  
  <p>After these commands and going into directory subdir/Iwant/to/put/B/in, I see all files of B, but <code>git log</code> on any one of them shows just  the commit message "Merge B as subdirectory in subdir/Iwant/to/put/B/in." Their file history as it is in B is lost.</p>
  
  <p>What <em>seems</em> to work (since I'm a beginner on git I may be wrong) is the following:</p>
  
  <pre><code>git remote add -f B &lt;url-of-B&gt;
  git checkout -b B_branch B/master  # make a local branch following B's master
  git filter-branch --index-filter \ 
     'git ls-files -s | sed "s-\t\"*-&amp;subdir/Iwant/to/put/B/in/-" |
          GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
                  git update-index --index-info &amp;&amp;
          mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' HEAD 
  git checkout master
  git merge B_branch
  </code></pre>
  
  <p>The command above for filter-branch is taken from <code>git help filter-branch</code>, in which I only changed the subdir path.</p>