title: How to move a file from one git repository to another while preserving history
body: <p>I am trying to move a single file (call it foo.txt) from one repository to another (unrelated) repository, preserving its history. <a href="https://stackoverflow.com/questions/1365541/how-to-move-files-from-one-git-repo-to-another-not-a-clone-preserving-history">ebneter's question</a> shows how to do this for a subdirectory. <a href="https://stackoverflow.com/questions/3192828/is-it-possible-to-move-a-bunch-of-files-from-one-git-repository-to-another-while">taw's question</a> has some hints and suggestions, but not a step-by-step procedure to follow. <a href="https://stackoverflow.com/questions/5998987/splitting-a-set-of-files-within-a-git-repo-into-their-own-repository-preserving">jkeating's question</a> looked promising, but didn't work for me. Google searches came up empty for this particular use case. What I am looking for is a clear sequence of commands to accomplish this.</p>
  
  <p>The sequence of commands I started to follow was this:</p>
  
  <pre><code>$ git clone source-repo/ source-repo-copy
  $ cd source-repo-copy
  $ git filter-branch --tree-filter 'test ! "$@" = "foo.txt" &amp;&amp; \
    git rm --cached --ignore-unmatch $@ || true' --prune-empty
  </code></pre>
  
  <p>The logic of my filter command is to <code>git rm</code> all files that are not foo.txt. I added the <code>|| true</code> to the command to force it to have a zero return value to satisfy filter-branch.</p>
  
  <p>My intention was to set source-repo-copy as a remote for my target repository (target-repo), and assuming <code>git filter-branch</code> filtered out everything but foo.txt, fetch and merge source-repo-copy into target-repo. Unfortunately, the <code>git filter-branch</code> command seemed to have no effect. It ran with no errors and appeared to grind through the 600+ commits in source-repo, but when it finished, the <code>git log</code> and files in source-repo-copy looked the same. Shouldn't all files but foo.txt be missing and all commits that didn't touch it be gone from the log?</p>
  
  <p>At this point I don't know how to proceed. Any suggestions?</p>